[Question solution] hdu 2094 produces the championship

topic

There is a group of people playing table tennis matches, catching and killing each other, at most one game between every two people.
The rules of the game are as follows:
if A beats B and B beats C again, and there is no match between A and C, then it is determined that A must beat C.
If A beats B, B beats C again, and C beats A again, then A, B, and C cannot become champions.
According to this rule, you may be able to determine the champion without the need for round-robin contests. Your task is to face a group of contestants and, after several rounds of killings, determine whether a championship has actually been produced.

Input
contains some player groups. Each group of players starts with an integer n (n<1000), followed by the match result of n pairs of players. The match result is represented by a pair of player names (with a space in between). If n is 0, it means the end of input.

Output
For each player group, if you determine that a champion is produced, output "Yes" in one line, otherwise output "No" in one line.

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

Ideas

Set can solve this problem well.
Define sets a, b, put all people into set a, and put all failed people into set b. If ab=1, it means that only one person has not failed, then This person is the champion

program

#include<bits/stdc++.h>
using namespace std;
int n;
string s1,s2;
int main()
{
    
    
	cin>>n;
	while (n!=0)
	{
    
    
		set <string> a,b;
		for (int i=1;i<=n;i++) 
		{
    
    
			cin>>s1>>s2;
			a.insert(s1); a.insert(s2);
			b.insert(s2);
		}
		if (a.size()-b.size()==1) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
		cin>>n;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45485187/article/details/108597715