产生冠军 HDU

分析: 当有且只有一个节点入度为0时,该节点即为冠军,否则不能产生冠军。所以以下代码中只要入度大于0的无论是几都将其设置为1。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <vector>
 6 #include <algorithm>
 7 #include <sstream>
 8 #include <map>
 9 
10 
11 using namespace std;
12  
13 
14 int main()
15 {
16     
17     int n;
18     while(cin >> n && n != 0)
19     {
20         map<string, int> mp;  // 分别为名字,入度
21         string a, b;
22         for(int i = 0; i < n; ++i)
23         {
24             cin >> a >> b;
25             mp[b] = 1;
26             if(mp[a] != 1)
27                 mp[a] = 0;
28         }
29         
30         int cnt = 0;    // 入度为0的节点个数 
31         for(map<string, int>::iterator it = mp.begin(); it != mp.end(); ++it)
32             if(it->second == 0)    
33                 cnt++;
34         
35         
36         if(cnt == 1)
37             cout << "Yes" << endl;
38         else
39             cout << "No" << endl;
40     }
41        
42     return 0;
43 }
原文地址:https://www.cnblogs.com/FengZeng666/p/11250150.html