hdu1054

#include<bits/stdc++.h>
using namespace std;
const int maxn=1505;
int vis[maxn];
vector<int> e[maxn];
int f[maxn][2];
void dfs(int x)
{
    f[x][1]=1;
    f[x][0]=0;
    for(int i=0; i<e[x].size(); i++)
    {
        int y=e[x][i];
        dfs(y);
        f[x][1]+=min(f[y][0],f[y][1]);
        f[x][0]+=f[y][1];
    }
    return;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=-1)
    {
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
        {
            int a,b;
            scanf("%d:(%d)",&a,&b);
            e[a].clear();
            for(int j=1; j<=b; j++)
            {
                int c;
                scanf("%d",&c);
                e[a].push_back(c);
                vis[c]=1;
            }
        }
        int root;
        for(int i=0; i<n; i++)
        {
            if(vis[i]==0)
            {
                root=i;
                break;
            }
        }
        dfs(root);
        printf("%d
",min(f[root][0],f[root][1]));
    }
}
原文地址:https://www.cnblogs.com/dongdong25800/p/10991432.html