多判断一个唯一性
only[ x] [0/1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #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() ; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!