Party at Hali-Bula UVA
来源:互联网 发布:手机淘宝可以开店吗 编辑:程序博客网 时间:2024/05/29 14:00
按照紫书的思路,递归求解即可,同时注意最终结束的条件是n=0,被这个点卡了很久,以下是源代码:
#include<iostream>#include<vector>#include<string>#include<set>#include<stack>#include<queue>#include<map>#include<algorithm>#include<cmath>#include<iomanip>#include<cstring>#include<sstream>#include<cstdio>#include<deque>using namespace std;int n;map<string, int> nameToId;int amount;int total[210][2];bool only[210][2];int getId(const string& name){if (nameToId.find(name) == nameToId.end())nameToId[name] = amount++;return nameToId[name];}bool judge(int ind){if (total[ind][0] == total[ind][1]) return false;else if (total[ind][0] > total[ind][1]) return only[ind][0];else return only[ind][1];}int dp(vector<int>* son,int ind,int select){total[ind][select] = select;only[ind][select] = true;for (int i = 0; i < son[ind].size(); i++){int index1 = son[ind][i];if (select){total[ind][select] += dp(son,index1,0);if (!only[index1][0]) only[ind][select] =false;}else{total[ind][select] += max(dp(son,index1,0),dp(son,index1,1));if (!judge(index1))only[ind][select] = false;}}return total[ind][select];}int main(){while (cin >> n&&n){nameToId.clear();string s;cin >> s;amount = 0;getId(s);string s1, s2;vector<int> son[210];for (int i = 1; i < n; i++){cin >> s1 >> s2;int id1 = getId(s1), id2 = getId(s2);son[id2].push_back(id1);}cout << max(dp(son, 0, 0), dp(son, 0, 1));if (judge(0)) cout << " Yes" << endl;else cout << " No" << endl;}return 0;}
阅读全文
0 0
- 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 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 OJ 1220 - Party at Hali-Bula
- UVA 1220 Party at Hali-Bula
- Party at Hali-Bula
- Party at Hali-Bula POJ3342
- poj3342 Party at Hali-Bula
- UVa1220 Party at Hali-Bula
- 新版本的全志R16平台的tinav2.1的系统打开softAP(分色排版)
- 看QT License了解 License协议
- MySQL信息函数
- git 提交
- oracle表相关操作
- Party at Hali-Bula UVA
- System.exit(1)和System.exit(0)的区别
- 理解全概率公式与贝叶斯公式
- D
- HDU A + B Problem II
- 二叉树系列:已知二叉树的中序遍历和前序遍历,求后序遍历
- svn参考
- Python面向对象编程
- JAVA中的反射机制