产生冠军 HDU - 2094

原题链接

考察:拓扑排序(?)

这道题压根不用判断是否成环如果判断了反而是错的(WA了3次),只需要判断入度==0的点是否只有一个就行

易错:

       判断成环 这组测试数据证明这个是错的 a b,b c,c b 这只是说明bc不能当冠军,但是a可以

反复提醒:迭代器元素是pair,如果要用first second要用->

和上道题一样,这个不用判重

 1 #include <iostream>
 2 #include <queue>
 3 #include <unordered_map>
 4 #include <string> 
 5 #include <map>
 6 using namespace std;
 7 const int N = 2010;
 8 unordered_map<string,int> um;
 9 map<int,int> mp;
10 int idx,h[N],e[N],ne[N],d[N],n,q[N];
11 void add(int a,int b)
12 {
13     e[idx]= b,ne[idx]=h[a],h[a]=idx++;
14 }
15 bool topsort()//在迭代器里用pair一定用指针 
16 {
17     int hh=0,tt=-1;
18     for(auto it=um.begin();it!=um.end();it++) if(!d[it->second]) q[++tt] = it->second;
19     if(tt!=hh) return false;
20     else return true;
21 }
22 int main()
23 {
24 //    freopen("in.txt","r",stdin);
25     while(scanf("%d",&n)!=EOF&&n)
26     {
27         um.clear();// mp.clear();
28         fill(h,h+N,-1); fill(d,d+N,0);idx = 0;
29         for(int i=1;i<=n;i++)
30         {
31             string a,b; cin>>a>>b;
32             if(!um.count(a)) um[a] = um.size();
33             if(!um.count(b)) um[b] = um.size();
34             add(um[a],um[b]);
35             d[um[b]]++;
36         }
37         if(topsort()) printf("Yes\n");
38         else printf("No\n");
39     }
40     return 0;
41 }

 

posted @ 2021-01-11 00:23  acmloser  阅读(61)  评论(0编辑  收藏  举报