HDU 2094 产生冠军

题目地址

题意:

我们假设cnt为有向图中,入度为0的点的数量,那么:

  1. 若cnt为0,显然没有冠军
  2. 若cnt>1,则每个入度为0的点都可以是冠军,结果就是没有冠军
  3. 若cnt=1,此时有唯一的冠军人选

思路:

统计入度为0的点的数量即可。采用map或set来灵活存图。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=3e3+5;

int in[maxn];

int main()
{
	cin.tie(0)->sync_with_stdio(false);
	int m;
	while(cin>>m && m)
	{	
		map<string,int> mp;
		vector<pair<string,string>> v;
		for(int i=1;i<=m;++i)
		{
			string a,b;
			cin>>a>>b;
			v.emplace_back(a,b);
			mp[a]=-1;
			mp[b]=-1;
		}
		int n=0;
		for(auto &p:mp)
		{
			p.second=++n;
			in[n]=0;
		}
		for(auto &p:v)
		{
			int x=mp[p.first], y=mp[p.second];
			++in[y];
		}
		int cnt=0;
		for(int i=1;i<=n;++i)
			if(!in[i])
				++cnt;
		if(cnt!=1)
			cout<<"No\n";
		else
			cout<<"Yes\n";
	}
}
posted @ 2023-01-02 11:14  ice_dragon_grass  阅读(42)  评论(0)    收藏  举报