hdoj 2094 产生冠军

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2094

//拓扑排序
/*
网上看到的一个比较好的方法,使用map,set处理。
利用set建立点集,用map映射形成边,用输的指向赢的 A <- B
由于题目是求产生冠军,只需要有向图只有一个根就可以了,即遍历set点集合时,查看是否有后继,且唯一
*/
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;

int main ()
{
	set<string> s;
	set<string>::iterator it;
	map<string, string> m;
	string a, b;
	int t;
	while (cin >> t, t) {
		s.clear();
		m.clear();
		while (t--) {
			cin >> a >> b;
			s.insert(a);
			s.insert(b);
			m[b] = a;
		}
		int i;
		for (i = 0, it = s.begin(); it != s.end(); ++it) {
			if (m[*it].length() == 0)//统计有多少个图的出口
				i ++;
		}
		puts((i == 1) ? "Yes" : "No");//只有一个出口则为所求
	}
	return 0;
}


你可能感兴趣的:(hdoj 2094 产生冠军)