WA了好几个小时,就是不知道问题在哪
结果查到一个解题报告里面说
这题刚开始提交的时候不断WA。
开始我是采用这种输入的!!
for(i=0;i<n;i++)
{
scanf("%s",buf);
int temp=buf[0]-'0';
int len= buf[3]-'0';
}
后来改为这种输入才AC。。汗!
结果确实如此。。。。。。。我勒个去。。。。。。。
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <queue>
#include <list>
#include <stack>
#include <iomanip>
using namespace std;
///
///
#define INF 20000000
#define maxn 1510
#define max(a,b)(a>b?a:b)
///
int n;
int dp[maxn][5];
int min(int a, int b)
{
if (a > b)
return b;
else
return a;
}
struct node{
vector<int> sons;
int level;
int pos;
};
bool operator < (const node &t1,const node &t2)
{
return t1.level > t2.level;
}
node tree[maxn];
char str[100];
int main()
{
///
int i, j;
while (scanf("%d", &n) != EOF)
{
int root;
for (i = 0; i < maxn; i++)
{
tree[i].level = 0;
tree[i].pos = 0;
tree[i].sons.clear();
}
for (i = 0; i < n; i++)
{
int from, nums;
scanf("%d:(%d)", &from, &nums);
if (i == 0)
{
tree[from].level = 1;
root = from;
}
tree[from].pos = from;
for (j = 0; j < nums; j++)
{
int to;
scanf("%d", &to);
tree[from].sons.push_back(to);
tree[to].level = tree[from].level + 1;
}
}
sort(tree, tree + n);
//具体计算,自底向上动态规划过程
memset(dp, 0, sizeof(dp));
for (i = 0; i < n; i++)
{
if (tree[i].sons.size() == 0)
{
dp[tree[i].pos][1] = 1;
dp[tree[i].pos][0] = 0;
continue;
}
int sum0 = 0;
int sum1 = 1;
for (j = 0; j < tree[i].sons.size(); j++)
{
//如果j是i的子结点,那么dp[i][0] = sigma(dp[j][1])
sum0 += dp[tree[i].sons[j]][1];
sum1 += min(dp[tree[i].sons[j]][0], dp[tree[i].sons[j]][1]);
}
dp[tree[i].pos][0] = sum0;
dp[tree[i].pos][1] = sum1;
}
//输出结果
printf("%d\n", min(dp[root][1], dp[root][0]));
// cout << << endl;
}
///
return 0;
}