UVa OJ 1220 - Party at Hali-Bula
来源:互联网 发布:c语言公开课 编辑:程序博客网 时间:2024/06/06 03:35
UVa OJ 1220 - Party at Hali-Bula
Problem
Here is the: problem link
Solution
这道题目一开始没有用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; }
0 0
- UVa OJ 1220 - Party at Hali-Bula
- Uva-1220-Party at Hali-Bula
- UVA 1220Party 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
- UVA-1220 Party at Hali-Bula
- UVA 1220(p282)----Party at Hali-Bula
- UVA 1220 Party at Hali-Bula
- Party at Hali-Bula UVA
- Party at Hali-Bula(UVA
- Party at Hali-Bula UVA
- Party at Hali-Bula UVA
- 【UVA】1220-Party at Hali-Bula?(动态规划,树)
- uva 1220 - Party at Hali-Bula 【入门树形dp】
- UVA 1220 / HDOJ 2412 Party at Hali-Bula 树形DP
- UVa #1220 Party at Hali-Bula (例题9-13)
- CSS学习笔记-margin,padding(八)
- android view滑动的几种方法
- html5 新增的input元素
- Jenkins+Git+Maven+Shell+Tomcat
- kmalloc第二个参数的意义
- UVa OJ 1220 - Party at Hali-Bula
- 1 the brain and behavior
- 我的第一篇博客生涯
- PID算法的C语言实现
- 用google zxing生成二维码和扫描二维码
- UNIX & LINUX学习之开始使用Unix
- 【Linux笔记】Linux目录结构
- su - 和 su root 区别
- 9月英语总结--更多的是反思