标签:
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2925 Accepted Submission(s): 1222
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
int brother,child;
int yes;//该点放士兵
int no; //该点不放士兵
}tree[1505];
int n,i,j,k,root,num,origin;
void dfs(int root)
{
int child=tree[root].child;
while(child>0)
{
dfs(child);
tree[root].yes+=min(tree[child].yes,tree[child].no);
//父亲结点放置了,儿子结点可以放置也可以不放置
tree[root].no+=tree[child].yes;
//父亲结点没有放置,儿子结点必须放置
child=tree[child].brother;
}
}
int main()
{
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
tree[i].brother=tree[i].child=0;
tree[i].yes=1;
tree[i].no=0;
}
for(int t=1;t<=n;t++)
{
scanf("%d:(%d)",&root,&num);
root++;
if (t==1) origin=root;
for(i=1;i<=num;i++)
{
int x;
scanf("%d",&x);
x++;
tree[x].brother=tree[root].child;
tree[root].child=x;
}
}
dfs(origin);
printf("%d\n",min(tree[origin].yes,tree[origin].no));
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/stepping/p/5539798.html