UVa1220 Party at Hali-Bula
2014-08-09 19:50
288 查看
这题应该算很经典的树上的动态规划。输入是人的姓名,我用了map和set来处理,转换为编号。题目的数据量比较小,甚至可以不用开dp数组。。
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> #include <iomanip> #include <cstdlib> #include <string> #include <memory.h> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <ctype.h> #define INF 1000000 #define ll long long #define min3(a,b,c) min(a,min(b,c)) using namespace std; vector<int> rel[210]; bool mul[210][2]; int fun(int x,bool f){ cout<<x<<endl; if(rel[x].size()==0){ return f; }else{ if(f){ int re=1; for(int i=0;i<rel[x].size();i++){ re+=fun(rel[x][i],0); if(mul[rel[x][i]][0])mul[x][1]=true; } return re; }else{ int re=0; for(int i=0;i<rel[x].size();i++){ int tmp0=fun(rel[x][i],0); int tmp1=fun(rel[x][i],1); re+=max(tmp0,tmp1); if(tmp0==tmp1)mul[x][0]=true; if(tmp0>tmp1&&mul[rel[x][i]][0])mul[x][0]=true; if(tmp0<tmp1&&mul[rel[x][i]][1])mul[x][0]=true; } return re; } } } int main(){ int n; while(cin>>n){ if(!n)break; for(int i=1;i<=n;i++)rel[i].clear(); memset(mul,0,sizeof(mul)); set<string> emp; map<string,int> m; string bigboss; cin>>bigboss; emp.insert(bigboss); m[bigboss]=1; int cnt=1; for(int i=1;i<n;i++){ string e,b; cin>>e>>b; if(!emp.count(e)){ emp.insert(e); m[e]=++cnt; } if(!emp.count(b)){ emp.insert(b); m[b]=++cnt; } rel[m[b]].push_back(m[e]); } bool flag=false; int tmp0=fun(1,0); int tmp1=fun(1,1); if(tmp0==tmp1)flag=true; if(tmp0>tmp1&&mul[1][0])flag=true; if(tmp0<tmp1&&mul[1][1])flag=true; cout<<max(tmp0,tmp1); if(flag) cout<<" No"<<endl; else cout<<" Yes"<<endl; } return 0; }
相关文章推荐
- UVa 1220 Party at Hali-Bula (树形DP,最大独立集)
- 100道动态规划——36 UVA 1220 Party at Hali-Bula 树形DP 最大独立集 思考欠妥
- UVA - 1220 Party at Hali-Bula 树的最大独立集
- UVa 1220 (树的最大独立集) Party at Hali-Bula
- UVA 1220 Party at Hali-Bula(树的最大独立集)
- 【UVA1220】Party at Hali-Bula
- 【Uva 1220】Party at Hali-Bula
- UVA 1220 / HDOJ 2412 Party at Hali-Bula 树形DP
- UVA - 1220 Party at Hali-Bula
- 【UVa1220】Party at Hali-Bula(树形DP)
- 紫书动规 例题9-13 UVA - 1220 Party at Hali-Bula 树形dp
- UVA - 1220 Party at Hali-Bula(树形dp)
- Party at Hali-Bula UVA - 1220 (简单树形dp)
- Uva-1220 - Party at Hali-Bula
- Party at Hali-Bula UVA - 1220
- POJ 3342 Party at Hali-Bula / HDU 2412 / UVAlive 3794 / UVA 1220
- uva 1220 Party at Hali-Bula 树形DP
- uva 1220——Party at Hali-Bula
- UVa1220 Party at Hali_Bula
- Uva-1220 Party at Hali-Bula