HDU 2094 产生冠军

传送门;http://acm.hdu.edu.cn/showproblem.php?pid=2094

解题思路:

若a打败b,那么a->b有一条边。若能产生冠军那么只有一个冠军的入度为0.其它的入度不为0.

实现代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <map>
 7 #include <queue>
 8 using namespace std;
 9 
10 /*********************************
11 注:
12 d a
13 a b
14 b c
15 c a
16 这也是YES
17 **********************************/
18 const int MAXN=2010;
19 int deg[MAXN];
20 map<string,int>mp;
21 
22 int main(){
23     int n;
24     while(scanf("%d",&n)!=EOF&&n){
25         mp.clear();
26         memset(deg,0,sizeof(deg));
27         int cnt=0;
28         for(int i=0;i<n;i++){
29             string str1,str2;
30             cin>>str1>>str2;
31             if(mp[str1]==0){
32                 mp[str1]=++cnt;
33             }
34             if(mp[str2]==0){
35                 mp[str2]=++cnt;
36             }
37             deg[mp[str2]]++;
38         }
39         int ans=0;
40         for(int i=1;i<=cnt;i++)
41             if(!deg[i])
42                 ans++;
43         if(ans==1)
44             cout<<"Yes"<<endl;
45         else
46             cout<<"No"<<endl;
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/IKnowYou0/p/6720446.html