hdu 2412 Party at Hali-Bula 树形dp
2014-04-08 23:47
288 查看
#include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> #include<map> using namespace std; map<string,int>ma; map<string,int>::iterator na; const int N =210; char str1 ,str2 ; int cnt; int dp [2]; bool uniq [2]; int find(string str){ na=ma.find(str); if(na==ma.end()){ ma[str]=cnt++; return ma[str]; } else { return ma[str]; } } vector<int>g ; void dfs(int root){ if(g[root].size()==0){ dp[root][0]=0; dp[root][1]=1; return; } for(int i=0;i<g[root].size();i++){ int u=g[root][i]; dfs(u); dp[root][0]+=max(dp[u][0],dp[u][1]); dp[root][1]+=dp[u][0]; if(uniq[root][0]==true){//这里掉了,错了n久,因为后面1,2那里 if(dp[u][0]==dp[u][1]){ uniq[root][0]=false; } else if(dp[u][0]>dp[u][1]){ uniq[root][0]=uniq[u][0];//1 } else { uniq[root][0]=uniq[u][1];//2 } } if(uniq[u][0]==false) uniq[root][1]=false; } dp[root][1]++; } int main(){ int n; while(scanf("%d",&n)!=EOF&&n){ scanf("%s",str1); ma.clear(); for(int i=0;i<=n;i++){ g[i].clear(); } ma[str1]=0; cnt=1; for(int i=1;i<n;i++){ scanf("%s%s",str1,str2); int fx=find(str1); int fy=find(str2); // printf("fx=%d fy=%d\n",fx,fy); g[fy].push_back(fx); } memset(dp,0,sizeof(dp)); memset(uniq,true,sizeof(uniq)); dfs(0); if(dp[0][0]==dp[0][1]) printf("%d No\n",dp[0][0]); else if(dp[0][0]>dp[0][1]){ if(uniq[0][0]==true) printf("%d Yes\n",dp[0][0]); else printf("%d No\n",dp[0][0]); } else { if(uniq[0][1]==true) printf("%d Yes\n",dp[0][1]); else printf("%d No\n",dp[0][1]); } } return 0; }
相关文章推荐
- hdu 2412 Party at Hali-Bula(树形DP)
- 【树形dp】hdu 2412 Party at Hali-Bula
- hdu 2412 Party at Hali-Bula 经典树形DP
- hdu - 2412 - Party at Hali-Bula(树形dp)
- 【hdu 2412 Party at Hali-Bula 】(树形dp求最大独立集+判断唯一性)
- 树形DP(1)-Hdu 2412 Party at Hali-Bula
- hdu 2412 Party at Hali-Bula(树形DP)
- HDU 2412 Party at Hali-Bula (树形DP + hash)
- 【树形DP】 HDU 2412 Party at Hali-Bula
- hdu 2412 Party at Hali-Bula(简单的树形dp)
- 【树形DP】 HDU 2412 Party at Hali-Bula
- HDU 2412 Party at Hali-Bula 树形DP
- hdu 2412 Party at Hali-Bula (树形DP)
- HDU 2412 Party at Hali-Bula 树形DP
- HDU 2412 Party at Hali-Bula (树形DP,map)
- HDU 2412 / POJ 3342 Party at Hali-Bula(树形DP+判断多解)
- Party at Hali-Bula+hdu+经典树形dp
- hdoj 2412 Party at Hali-Bula 【树形dp】
- HDU 2412 Party at Hali-Bula(树状DP)
- hdoj--2412 Party at Hali-Bula(树形dp)