描述:
类似于最大独立集,略。
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
const int MAX = 210;
int n,num,d[MAX][2],f[MAX][2];
vector<int> sub[MAX];
map<string,int> dict;//精髓就是建树的过程
int code(const string& s){//const + 引用 提高效率
if(!dict.count(s))
dict[s] = num++;
return dict[s];
}
int dp(int p,int k){
int i;
if(sub[p].empty()){
f[p][k] = 0;
return d[p][k] = k;
}
int l = sub[p].size();
d[p][k] = k;
f[p][k] = 0;
if(k){
for(i = 0;i < l; ++i){
d[p][1] += dp(sub[p][i],0);
if(f[sub[p][i]][0])
f[p][1] = 1;
}
}
else{
for(i = 0;i < l; ++i){
int t = sub[p][i];
d[p][0] += max(dp(t,0),dp(t,1));
if(d[t][0] == d[t][1]){
f[p][0] = 1;
}
else if(d[t][0] > d[t][1] && f[t][0]){
f[p][0] = 1;
}
else if(d[t][0] < d[t][1] && f[t][1]){
f[p][0] = 1;
}
}
}
return d[p][k];
}
int main(){
//freopen("d://poj//input.txt","r",stdin);
//freopen("d://poj//data.txt","w",stdout);
int i;
string s,s2;
while(cin >> n && n){
num = 0;
dict.clear();
for(i = 0;i <= n; ++i)
sub[i].clear();
cin >> s;
code(s);
for(i = 1;i < n; ++i){
cin >> s >> s2;
sub[code(s2)].push_back(code(s));
}
printf("%d ",max(dp(0,0),dp(0,1)));
int flag = 0;
// if((d[0][1] > d[0][0] && !f[0][1]) || (d[0][0] > d[0][1] && !f[0][0]))
// flag = 1;
// if(flag)
// printf("Yes\n");
// else
// printf("No\n");
if(d[0][0] == d[0][1])//莫名其妙,浪费我时间,f**kuasshole
printf("No\n");
else if(d[0][0] > d[0][1]){
if(f[0][0])
printf("No\n");
else
printf("Yes\n");
}
else{
if(f[0][1])
printf("No\n");
else
printf("Yes\n");
}
}
return 0;
}
服务器,你给我翻译翻译,什么叫做他妈是他妈的WA
???