标签:des style http color io os ar for sp
4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0)
1 2一般的树形DP;
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> #include<vector> typedef long long LL; using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) vector<int>v[1500]; int vis[1500]; int dp[1500][2]; int n,m; void dfs(int x) { if(vis[x]) return ; vis[x]=1; dp[x][1]=1; dp[x][0]=0; for(int i=0;i<v[x].size();i++) { if(!vis[v[x][i]]) { dfs(v[x][i]); dp[x][0]+=dp[v[x][i]][1]; dp[x][1]+=min(dp[v[x][i]][1],dp[v[x][i]][0]); } } } int main() { int x,y,temp; while(~scanf("%d",&n)) { REP(i,n) v[i].clear(); REPF(i,1,n) { scanf("%d:(%d)",&x,&y); while(y--) { scanf("%d",&temp); v[x].push_back(temp); v[temp].push_back(x); } } CLEAR(vis,0); dfs(1);//由于是无向图可以在0~n中随变找一个点 printf("%d\n",min(dp[1][1],dp[1][0])); } return 0; }
标签:des style http color io os ar for sp
原文地址:http://blog.csdn.net/u013582254/article/details/40454601