题目传送:HDU - 2094 - 产生冠军
思路:因为只要确定是否产生冠军,根据题意可以得知当且仅当入度为0的只有一个时可以产生冠军,可以用map来映射存储的人的名字
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
map<string, int> mp;
int deg[1005];
int cnt;
int n;
int main() {
while(scanf("%d", &n) != EOF) {
if(n == 0) break;
memset(deg, 0, sizeof(deg));
cnt = 1;
mp.clear();
string s1, s2;
for(int i = 0; i < n; i ++) {
cin >> s1 >> s2;
// cout << s1 << " " << s2 << endl;
if(mp.find(s1) == mp.end()) {
mp[s1] = cnt ++;
}
if(mp.find(s2) == mp.end()) {
mp[s2] = cnt ++;
}
int t1 = mp[s1];
int t2 = mp[s2];
deg[mp[s2]] ++;
}
int sum = 0;
for(int i = 1; i < cnt; i ++) {
if(deg[i] == 0) {
sum ++;
}
}
if(sum == 1) {
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u014355480/article/details/47205701