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 ?nds 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
6 Jason Jack Jason Joe Jack Jill Jason John Jack Jim Jill 2 Ming Cho Ming 0
Sample Output
4 Yes 1 No
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
#define maxn 205
#define MAX 1000000000
#define ms memset
using namespace std;int n,cnt;
int flag[maxn][2];///从属关系
int dp[maxn][2];///0代表不选根,1代表选根
/*
题目大意:n个人形成树状结构,除老板外每个员工都有唯一的编号,
选尽量多的人但不能同时选择他和其上属,且选法是否唯一也要输出。树状的最大独立集的问题。
d(u,1),d(u,0)分别表示选根和没选根的子树的最大独立集。
那么递推过程可分析出来,d(u,1)选了根则子节点均不能选,
所以答案为其子节点sigma d(sons,0),其中如果有一个子节点答案不唯一则根节点不唯一。如果没选根,子节点可选可不选,sigma (max(d(sons,0),d(sons,1)));
其中的flag如果子节点两种状态都一样则flag为0,如果不一样则判断选定的那个子节点的状态即可过程中我用的记忆化搜索,因为状态可能有重叠,因为是树状结构,dfs复杂度不会太高
*/
vector<int> sons[maxn];
void read()
{memset(flag,0,sizeof(flag));memset(dp,0xff,sizeof(dp));for(int i=0;i<maxn;i++) sons[i].clear();char tp[maxn][maxn]; cnt=0;char name1[maxn] , name2[maxn];scanf("%s",name1);strcpy(tp[cnt++],name1);for(int i=0;i<n-1;i++){scanf("%s%s",name1,name2);int u=0;for(;u<cnt;u++)if(strcmp(tp[u],name1)==0) break;if(u==cnt) strcpy(tp[cnt++],name1);int v=0;for(;v<cnt;v++)if(strcmp(tp[v],name2)==0) break;if(v==cnt) strcpy(tp[cnt++],name2);sons[v].push_back(u);}for(int i=0;i<cnt;i++)if(sons[i].empty()){dp[i][0]=0;dp[i][1]=1;flag[i][0]=1,flag[i][1]=1;///一个点时独立集是唯一的}
}int Dp(int u,int v)///记忆化搜索
{if(dp[u][v]>-1) return dp[u][v];if(sons[u].empty()) return v;int len=sons[u].size();int res1=0 , res2=0;flag[u][v]=1;if(v){res1++;for(int i=0;i<len;i++){res1+= Dp(sons[u][i],0);flag[u][v] &= flag[sons[u][i]][0];}return dp[u][v]=res1;}else{dp[u][v]=0;for(int i=0;i<len;i++){int res1= Dp(sons[u][i],0);int res2= Dp(sons[u][i],1);dp[u][v]+=max(res1,res2);if(res1==res2) flag[u][v]=0;else if(res1>res2&& flag[sons[u][i]][0]==0) flag[u][v]=0;else if(res1<res2 && flag[sons[u][i]][1]==0) flag[u][v]=0;}return dp[u][v];}
}int main()
{while(scanf("%d",&n) && n){read();int ans1=Dp(0,0);int ans2=Dp(0,1);if(ans1==ans2){printf("%d ",ans1);puts("No");}else if(ans1>ans2){printf("%d ",ans1);if(flag[0][0]) puts("Yes");else puts("No");}else if(ans2>ans1){printf("%d ",ans2);if(flag[0][1]) puts("Yes");else puts("No");}}return 0;
}