多判断一个唯一性
only[ x] [0/1]
#include <iostream> #include <cstring> #include <vector> #include <map> #include <algorithm> using namespace std; const int N=205; int f[N][2],n,K; int only[N][3] ; vector<int> g[N]; map<string,int> mp; void dfs(int x){ f[x][1]=1; f[x][0]=0; for(auto y:g[x]){ dfs(y); f[x][0]+=max(f[y][0],f[y][1]); f[x][1]+=f[y][0]; } for(auto v:g[x]){ if(f[v][1]>f[v][0]&&only[v][1]) only[x][0]=1; else if(f[v][0]==f[v][1]) only[x][0]=1; else if(f[v][1]<f[v][0]&&only[v][0]) only[x][0]=only[x][1]=1; } } int len; void sov(){ len=0; mp.clear(); for(int i=0;i<N;i++) g[i].clear() ; memset(only,0,sizeof only ); memset(f,0,sizeof f) ; string s1,s2; cin>>s1; mp[s1]=++len; for(int i=1;i<n;i++){ cin>>s1>>s2; if(mp.count(s1)==0) mp[s1]=++len; if(mp.count(s2)==0) mp[s2]=++len; // cout<<"add " <<mp[s2]<<' '<<mp[s1]<<endl; g[mp[s2]].push_back(mp[s1]); } dfs(1) ; if(f[1][0]<f[1][1]){ printf("%d ",f[1][1]); puts(only[1][1]?"No":"Yes"); } else if(f[1][0]>f[1][1]){ printf("%d ",f[1][0]); puts(only[1][0]?"No":"Yes"); } else { printf("%d ",f[1][0]); puts("No"); } } int main(){ while(cin>>n&&n) sov() ; }
标签:int,s2,s1,only,mp,Hali,Party,Bula,include From: https://www.cnblogs.com/towboa/p/17318843.html