树的最大独立集问题
d(u,0) 表示以u为根的子树中,不选u点能得到的最大人数 f(u,0)=1表示唯一,否则不唯一
d(u,1)--------------------------------选u f(u,1)=1表示唯一,否则不唯一
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200+5;
int n,cnt,d[maxn][2],f[maxn][2];
vector<int> sons[maxn];
map<string,int> dict;
int ID(string s){
if(!dict.count(s)) dict[s] = cnt++;
return dict[s];
}
int dp(int u,int k){
d[u][k] = k;
f[u][k] = 1;
for(int i=0; i<sons[u].size(); i++){
int v = sons[u][i];
if(k==1){
d[u][k] += dp(v,0);
if(!f[v][0]) f[u][k] = 0;
}else{
d[u][k] += max(dp(v,0),dp(v,1));
if(d[v][0]==d[v][1]) f[u][k] = 0;
else if(d[v][0]>d[v][1] && !f[v][0]) f[u][k] = 0;
else if(d[v][1]>d[v][0] && !f[v][1]) f[u][k] = 0;
}
}
return d[u][k];
}
int main(){
string s,s2;
while(cin >> n >> s){
cnt = 0;
for(int i=0; i<n; i++) sons[i].clear();
dict.clear();
ID(s);
for(int i=0; i<n-1; i++){
cin >> s >> s2;
sons[ID(s2)].push_back(ID(s));
}
int ans;
ans = max(dp(0,0),dp(0,1));
printf("%d ", ans);
bool unique = false;
if(d[0][0]>d[0][1] && f[0][0]) unique = true;
if(d[0][1]>d[0][0] && f[0][1]) unique = true;
if(unique) printf("Yes\n");
else printf("No\n");
}
}