- map把字符转化一下数字对应上编号。
- 统计 如度为一的点。只有入度为一得点唯一的情况才是正确的拓扑序列,
-
#include<bits/stdc++.h> using namespace std; #define maxn 1006 map<string,int>mmp; string one,two; int t,cnt=1,in[maxn]= {0}; bool topo() { queue<int>q; for(int i=1; i<cnt; i++) { if(in[i]==0) q.push(i); if(q.size()>1) return false; } if(q.size()==0) return false; return true ; } int main() { while(cin>>t) { cnt=1; if(t==0) break; mmp.clear(); memset(in,0,sizeof(in)); while(t--) { cin>>one>>two; if(mmp[one]==0) mmp[one]=cnt++; if(mmp[two]==0) mmp[two]=cnt++; in[mmp[two]]++; } if(topo()) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
C - 产生冠军 HDU - 简单拓扑
最新推荐文章于 2023-03-23 12:44:27 发布