Question:
Champion
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4716 Accepted Submission (s): 2274
Problem Description
There are a group of people playing a table tennis competition, and they can play at most one game between each other.
The rules of the game are as follows:
If A beat B, B beat C again, and A and C do not compete, then it is determined that A will defeat C.
If A beat B, B beat C, and C beat A, then A, B, and C won't be the champion.
According to this rule, you may be able to determine the champion without repeating the contest. Your task is to face a group of contestants. After several battles, determine whether they have actually won the championship.
Input
The input contains some contestant groups. Each contestant group starts with an integer n (n <1000) and is followed by the result of the n-to-n match, the results of the competition are represented by a pair of contestants (separated by a space). The former prevails over the latter. If n is 0, the input ends.
Output
For each contestant group, if you determine that you have won the championship, "Yes" is output in one row; otherwise, "No" is output in one row ".
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
// Train of thought: record all the names that have appeared, and check whether the last one has been lost. If not, t ++ determines whether t = 1?
// Stl knowledge connection http://blog.csdn.net/xiongheqiang/article/details/7769633
AC code:
[Cpp]
# Include <iostream>
Using namespace std;
# Include <string>
# Include <map>
# Include <set> // when the container inputs two identical elements, only one
Int main ()
{
Int n, I;
Set <string> name; // put all existing names in the set container.
Set <string>: iterator r; // set pointer
Map <string, string> com;
While (cin> n, n! = 0)
{
String a, B;
Name. clear ();
Com. clear ();
For (I = 0; I <n; I ++)
{
Cin> a> B;
Name. insert ();
Name. insert (B );
Com [B] = a; // mark B lost to
}
Int t = 0; // t indicates the number of students not lost.
For (r = name. begin (); r! = Name. end (); r ++)
If (! Com [* r]. length () // If com [B] is not input, the length is 0.
{
T ++;
If (t> 1)
Break;
}
Puts (t = 1? "Yes": "No ");
}
Return 0;
}
Author: xiongheqiang