hdu 2094 产生冠军

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2094

产生冠军

Description

有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。

Input

输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。

Output

对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。

Sample Input

3
Alice Bob
Smith John
Alice Smith
5
a c
c d
d e
b e
a d
0

Sample Output

Yes
No

找入度为0的点吧,如果大于1则无法产生冠军。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<string>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::string;
17 using std::vector;
18 using std::multimap;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 1010;
29 typedef unsigned long long ull;
30 struct TopSort {
31     int k, inq[N];
32     map<string, int> rec;
33     inline void init() {
34         k = 0;
35         rec.clear(), cls(inq, 0);
36     }
37     inline void built(int m) {
38         int  a;
39         char str1[20], str2[20];
40         rep(i, m) {
41             scanf("%s %s", str1, str2);
42             if (rec.find(str1) == rec.end()) rec[str1] = k++;
43             if (rec.find(str2) == rec.end()) rec[str2] = k++;
44             a = rec[str2];
45             inq[a]++;
46         }
47     }
48     inline void solve() {
49         int ans = 0;
50         rep(i, sz(rec)) {
51             if (!inq[i]) ans++;
52         }
53         puts(ans == 1 ? "Yes" : "No");
54     }
55 }work;
56 int main() {
57 #ifdef LOCAL
58     freopen("in.txt", "r", stdin);
59     freopen("out.txt", "w+", stdout);
60 #endif
61     int n;
62     while (~scanf("%d", &n) && n) {
63         work.init();
64         work.built(n);
65         work.solve();
66     }
67     return 0;
68 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4637523.html