UVa OJ 1220 - Party at Hali-Bula
2016-09-30 23:03
316 查看
UVa OJ 1220 - Party at Hali-Bula
Problem
Here is the: problem linkSolution
这道题目一开始没有用f[][]来标记是否重复,导致WA了一次,后来就加上去了。另外,在处理人名时,虽然实例输入里,上司和下属名字出现是有先后的,但是提交之后,系统的测试数据貌似不一定是这样,所以要先判断是否出现,没出现就用cnt加1再赋值递归的思路还是不难的,选了上司,那么直属员工就不选,用d[][0]表示不选;没选的话就可以选直属员工,也可以不选,分别是d[][1]和d[][0]
#include <iostream> #include <cstdio> #include <map> #include <vector> #include <cstring> using namespace std; #define G sons[b][i] const int maxn = 205; int n, res, yn, d[maxn][2], f[maxn][2]; char c1[maxn], c2[maxn]; map<string, int> p; vector<int> sons[maxn]; int dp(int b,int y) { int& ans = d[b][y]; if (ans != -1) return ans; int k = sons[b].size(); if(!k) {f[b][y] = 1; return ans = y;} int mark = 1; if(y) { for(int i = 0; i < k; ++i) { ans += dp(G,0); if(!f[G][0]) mark = 0; } } else { for(int i = 0; i < k; ++i) { if(dp(G, 0) == dp(G, 1)) {mark = 0; ans += d[G][0];} else if(d[G][0] > d[G][1]) { if(!f[G][0]) mark = 0; ans += d[G][0]; } else { if(!f[G][1]) mark = 0; ans += d[G][1]; } } } f[b][y] = mark; return ans += y+1; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); //freopen("input.txt" , "r", stdin ); //freopen("output.txt", "w", stdout); while(cin >> n && n) { p.clear(); memset(d, 0xff, sizeof(d)); for(int i = 0;i < n; ++i) sons[i].clear(); cin >> c2; p[c2] = 0; int cnt = 0; for(int i = 1;i < n; ++i) { cin >> c1 >> c2; if(!p.count(c1)) p[c1] = ++cnt; if(!p.count(c2)) p[c2] = ++cnt; sons[p[c2]].push_back(p[c1]); } if(dp(0,0) > dp(0,1)) {res = d[0][0]; yn = f[0][0];} else if(d[0][0] == d[0][1]) {res = d[0][0]; yn = 0;} else {res = d[0][1]; yn = f[0][1];} cout << res; if(yn) cout << " Yes\n"; else cout << " No\n"; } return 0; }
相关文章推荐
- UVa 1220 (树的最大独立集) Party at Hali-Bula
- POJ 3342 Party at Hali-Bula / HDU 2412 Party at Hali-Bula / UVAlive 3794 Party at Hali-Bula / UVA 1220 Party at Hali-Bula(树型动态规划)
- UVA-1220 Party at Hali-Bula (树的最大独立集)
- UVa1220 - Party at Hali-Bula
- UVA1220Party at Hali-Bula(树形dp)
- 【Uva 1220】Party at Hali-Bula
- UVA-1220 Party at Hali-Bula
- UVA 1220 Party at Hali-Bula (树形dp--树的最大独立集)
- UVA 1220 Party at Hali-Bula
- uva 1220 - Party at Hali-Bula 【入门树形dp】
- UVa #1220 Party at Hali-Bula (例题9-13)
- 1220 - Party at Hali-Bula(Hali-Bula 的晚会)
- UVA1220[Party at Hali-Bula] 树上动态规划
- uva 1220——Party at Hali-Bula
- 【uva】1220 Party at Hali-Bula
- UVa 1220 Party at Hali-Bula
- 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(最大独立集)