您的位置:首页 > 其它

hdu 2412 Party at Hali-Bula 树形dp

2014-04-08 23:47 288 查看
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
map<string,int>ma;
map<string,int>::iterator na;
const int N =210;
char str1
,str2
;
int cnt;
int dp
[2];
bool uniq
[2];
int find(string str){
na=ma.find(str);
if(na==ma.end()){
ma[str]=cnt++;
return ma[str];
}
else {
return ma[str];
}
}
vector<int>g
;
void dfs(int root){
if(g[root].size()==0){
dp[root][0]=0;
dp[root][1]=1;
return;
}
for(int i=0;i<g[root].size();i++){
int u=g[root][i];
dfs(u);
dp[root][0]+=max(dp[u][0],dp[u][1]);
dp[root][1]+=dp[u][0];
if(uniq[root][0]==true){//这里掉了,错了n久,因为后面1,2那里
if(dp[u][0]==dp[u][1]){
uniq[root][0]=false;
}
else if(dp[u][0]>dp[u][1]){
uniq[root][0]=uniq[u][0];//1
}
else {
uniq[root][0]=uniq[u][1];//2
}
}
if(uniq[u][0]==false)
uniq[root][1]=false;
}
dp[root][1]++;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF&&n){
scanf("%s",str1);
ma.clear();
for(int i=0;i<=n;i++){
g[i].clear();
}
ma[str1]=0;
cnt=1;
for(int i=1;i<n;i++){
scanf("%s%s",str1,str2);
int fx=find(str1);
int fy=find(str2);
//	printf("fx=%d  fy=%d\n",fx,fy);
g[fy].push_back(fx);
}
memset(dp,0,sizeof(dp));
memset(uniq,true,sizeof(uniq));
dfs(0);
if(dp[0][0]==dp[0][1]) printf("%d No\n",dp[0][0]);
else if(dp[0][0]>dp[0][1]){
if(uniq[0][0]==true) printf("%d Yes\n",dp[0][0]);
else printf("%d No\n",dp[0][0]);
}
else {
if(uniq[0][1]==true) printf("%d Yes\n",dp[0][1]);
else printf("%d No\n",dp[0][1]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: