poj3342-Party at Hail-Bula 树形dp/树的最大独立集
来源:互联网 发布:淘宝客自动采集软件 编辑:程序博客网 时间:2024/04/28 18:47
Description
Dear Contestant,
I'm going to have a party at my villa at Hali-Bula to celebrate my retirement from BCM. I wish I could invite all my co-workers, but imagine how an employee can enjoy a party when he finds his boss among the guests! So, I decide not to invite both an employee and his/her boss. The organizational hierarchy at BCM is such that nobody has more than one boss, and there is one and only one employee with no boss at all (the Big Boss)! Can I ask you to please write a program to determine the maximum number of guests so that no employee is invited when his/her boss is invited too? I've attached the list of employees and the organizational hierarchy of BCM.
Best,
--Brian Bennett
P.S. I would be very grateful if your program can indicate whether the list of people is uniquely determined if I choose to invite the maximum number of guests with that condition.
Input
The input consists of multiple test cases. Each test case is started with a line containing an integer n (1 ≤ n ≤ 200), the number of BCM employees. The next line contains the name of the Big Boss only. Each of the following n-1 lines contains the name of an employee together with the name of his/her boss. All names are strings of at least one and at most 100 letters and are separated by blanks. The last line of each test case contains a single 0.
Output
For each test case, write a single line containing a number indicating the maximum number of guests that can be invited according to the required condition, and a word Yes or No, depending on whether the list of guests is unique in that case.
Sample Input
6JasonJack JasonJoe JackJill JasonJohn JackJim Jill2MingCho Ming0
Sample Output
4 Yes1 No
Source
紫书例题。
用f数组表示唯一性的dp值得学习。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<map>using namespace std;const int maxn=1e3+7;#define ll long longint n;int fa[maxn];vector<int> so[maxn];int d[maxn][2];int f[maxn][2];int rt;int num;string s, t;map<string, int> node;void init(){ node.clear(); rt=0; num=0; for(int i=0;i<maxn;i++) so[i].clear(); memset(d, -1, sizeof(d)); memset(f, -1, sizeof(f)); memset(fa, 0, sizeof(fa));}int ID(string & s){ map<string, int>::iterator it=node.find(s); if(it != node.end()) return it->second; node.insert(make_pair(s, ++num)); return num;}void dfs(int cur){ d[cur][0]=0; d[cur][1]=1; f[cur][0]=f[cur][1]=1; if(so[cur].size() == 0){ d[cur][0]=0; d[cur][1]=1; f[cur][0]=f[cur][1]=1; return ; } for(vector<int>::iterator it=so[cur].begin();it != so[cur].end();it++){ if(d[*it][0] == -1) dfs(*it); if(d[*it][1] == -1) dfs(*it); d[cur][0] += max(d[*it][0], d[*it][1]); if(d[*it][0] == d[*it][1]) f[cur][0]=0; else if(d[*it][0]>d[*it][1]){ if(f[*it][0] == 0) f[cur][0]=0; } else { if(f[*it][1] == 0) f[cur][0]=0; } if(d[*it][0] == -1) dfs(*it); d[cur][1] += d[*it][0]; if(f[*it][0] == 0) { f[cur][1]=0; } } //if(f[cur][0] == -1) f[cur][0]=1; //if(f[cur][1] == -1) f[cur][1]=1;}void solve(){ dfs(rt); printf("%d ", max(d[rt][0], d[rt][1])); if(d[rt][0]>d[rt][1]){ if(f[rt][0] == 1) printf("Yes\n"); else printf("No\n"); } else if(d[rt][0]<d[rt][1]){ if(f[rt][1] == 1) printf("Yes\n"); else printf("No\n"); } else printf("No\n");}int main(){ while(~scanf("%d", &n)&&n){ init(); cin>>s; rt=ID(s); for(int i=0;i<n-1;i++){ cin>>s>>t; int a=ID(s); int b=ID(t); fa[a]=b; so[b].push_back(a); } solve(); }}
- poj3342-Party at Hail-Bula 树形dp/树的最大独立集
- POJ3342 Party at Hali-Bula(树的最大独立集-树形DP-刷表法)
- POJ3342 Party at Hali-Bula 树形DP
- POJ3342 Party at Hali-Bula(树形DP)
- POJ 3342 Party at Hali-Bula (树形dp 树的最大独立集 判多解 好题)
- UVA 1220 Party at Hali-Bula (树形dp--树的最大独立集)
- POJ3342 Party at Hali-Bula(树型DP求最大独立集+唯一解判断)
- HDU2412 && POJ3342:Party at Hali-Bula(树形DP)
- 树形DP解 POJ3342-Party at Hali-Bula
- hdu Party at Hali-Bula 树形最大独立集 刷表递推解
- POJ 3342 Party at Hali-Bula 树形DP 最大独立集
- UVa - 1220 - Party at Hali-Bula ( 树形DP 求最大独立集 )
- UVa 1220 Party at Hali-Bula dp:树的最大独立集
- 100道动态规划——36 UVA 1220 Party at Hali-Bula 树形DP 最大独立集 思考欠妥
- 【树形动态规划】poj3342 Party at Hali-Bula
- Party at Hali-Bula POJ3342
- poj3342 Party at Hali-Bula
- poj3342 Party at Hali-Bula
- struts1.x入门笔记
- 抓包工具
- ACdream 1100 瑶瑶饿了(Folyd+完全背包问题)
- 求n的阶乘(while循环版)
- 物料主数据(SAP屠夫)
- poj3342-Party at Hail-Bula 树形dp/树的最大独立集
- Java 约瑟夫环问题解决方案
- 通过RunLoop避免一次闪退
- CGI与FastCGI
- Swift 学习网址
- 信息增益(比)的算法
- Dialog关闭问题
- 黑马程序员——多线程(二)
- thinkphp在非核心文件中引入第三方文件