HDU 1412 参加聚会 (map+树形dp)

来源:互联网 发布:安卓用什么软件编程 编辑:程序博客网 时间:2024/05/09 01:03


题目大意:

n个人形成一个关系树,每个节点代表一个人,节点的根表示这个人的唯一的直接上司,只有根没有上司。要求选取一部分人出来,使得每2个人之间不能有直接的上下级的关系,
求最多能选多少个人出来,并且求出获得最大人数的选人方案是否唯一。
前半部分很容易求得,直接一个树形DP,后面的判断最优解是否唯一比较难搞。。
看了roba的ppt顿时恍然大悟。
 
§
新加一个状态
dup[i][j],表示相应的dp[i][j]是否是唯一方案。
§对于叶子结点, dup[k][0] = dup[k][1] = 1.
§对于非叶子结点,
对于i的任一儿子j,若(dp[j][0] > dp[j][1]  dup[j][0] == 0)  (dp[j][0] < dp[j][1]  dup[j][1] == 0)  (dp[j][0] == dp[j][1]),则dup[i][0] = 0
对于I的任一儿子jdup[j][0] = 0, 
dup[i][1] = 0
dup[i][1]=0表示dp[i][1]不唯一;dup[i][1]=1表示dp[i][1]唯一;
dup[i][0]同上。
这样思路就很清晰了。
这个题如果是我出的话,我会加以改进,把字母的长度缩短,然后n稍微变大,这样前面需要用到字典树来查找。但是不知道n变大之后对dp的影响会不会很大
会不会使程序超时我就不得而解了。
代码:


Problem 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


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>using namespace std;map<string,int>number;map<int,string>temp;vector<int>vv[222];int vis[222],dp[222][2],state[222][2];void dfs(int r){vis[r]=1;int i,j,len=vv[r].size();dp[r][1]=1;for(i=0;i<len;i++) {int son=vv[r][i];if(vis[son]) continue;dfs(son);//父节点不选if(dp[son][0]==dp[son][1]) state[r][0]=1;else if(dp[son][0]>dp[son][1] && state[son][0]) state[r][0]=1;else if(dp[son][0]<dp[son][1] && state[son][1]) state[r][0]=1;dp[r][0]+=max(dp[son][0],dp[son][1]); //父节点选if(state[son][0]) state[r][1]=1;dp[r][1]+=dp[son][0];}}int main(){string root,u,v;int k,i,j,n,m;while(cin>>n && n) {k=0;cin>>root;        number[root]=++k;temp[k]=root;for(i=1;i<n;i++) {cin>>u>>v;if(number[u]==0) {number[u]=++k;temp[k]=u;}if(number[v]==0) {number[v]=++k;temp[k]=v;}vv[number[u]].push_back(number[v]);vv[number[v]].push_back(number[u]);}memset(dp,0,sizeof(dp));memset(vis,0,sizeof(vis));memset(state,0,sizeof(state));dfs(number[root]);  cout<<max(dp[1][0],dp[1][1]);if(dp[1][0]>dp[1][1]) {if(state[1][0]) cout<<" No"<<endl;else cout<<" Yes"<<endl;}else if(dp[1][0]<dp[1][1]) {if(state[1][1]) cout<<" No"<<endl;else cout<<" Yes"<<endl;}else cout<<" No"<<endl;for(i=1;i<=n;i++) {number[temp[i]]=0;vv[i].clear();}}return 0;}





0 0
原创粉丝点击