poj 1463树形dp 树的最小覆盖
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector> using namespace std; // push_back inline int read(){ int sum=0,x=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') x=0; ch=getchar(); } while(ch>='0'&&ch<='9'){ sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar(); } return x?sum:-sum; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } int mi(int x,int y){ return x<y?x:y; } int ma(int x,int y){ return x>y?x:y; } const int M=1505; const int inf=0x3f3f3f3f; vector<int>g[M]; int dp[M][2],f[M]; void dfs(int u){ dp[u][0]=0,dp[u][1]=1; for(int i=0;i<g[u].size();i++){ int v=g[u][i]; dfs(v); dp[u][1]+=mi(dp[v][1],dp[v][0]); dp[u][0]+=dp[v][1]; } } int main(){ char ch,ch1,ch2; int n; while(~scanf("%d",&n)){ for(int i=0;i<=n;i++) f[i]=0; for(int i=1;i<=n;i++){ int u,k; scanf("%d:(%d)",&u,&k); // cout<<k<<endl; u++; g[u].clear(); while(k--){ int v=read(); // cout<<"~~"<<v<<endl; v++; g[u].push_back(v); f[v]=u; // son[u]=v; } } /*for(int i=1;i<=n;i++) dp[i][0]=0,dp[i][1]=1;*/ int root=1; while(f[root]) root=f[root]; //cout<<root<<endl; dfs(root); write(mi(dp[root][0],dp[root][1])); putchar('\n'); } return 0; }