Click to open Hangzhou electric 2094
problem Description A group of people, playing table tennis game, 22 catch to kill, every two people play a maximum of a game.
The rules of the game are as follows:
If a defeated B,b and defeated C, and A and C did not play, then it is decided that a must be able to defeat C.
If a defeated B,b and defeated C, and C defeated A, then a, B, C Three will not be the champion.
According to this rule, there is no need to cycle a contest, perhaps to determine the championship. Your task is to face a group of contestants, after a number of killings, to determine whether it has actually produced a championship.
input inputs contain a number of contestants, each of which starts with an integer n (n<1000) followed by N against the competitor, and the result is a pair of player names (one space between them), the former defeating the latter. If n is 0, it indicates the end of the input.
output for each player group, if you determine that a winner is generated, export "Yes" in one line, otherwise output "No" in a 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: The first method, will win people put in a group, the loser put in another group, to see the winning group does not appear in the loss of a group of the number, if it is 1, it can produce a championship, otherwise not (this method can be in Hangzhou electric AC)
The second method, each person as a node, statistics of each node into the degree, see all the nodes in the degree of 0 of the number of how many, if 1, it can produce a champion, otherwise not (this method can not AC, super memory, when practicing topological sorting)
Method One:
Package search.topology;
Import Java.util.Scanner;
public class P2094 {public static void main (string[] args) {Scanner sc=new Scanner (system.in);
while (Sc.hasnext ()) {int n=sc.nextint ();
if (n==0) {break;
} containter winners=new Containter (n);//To place the winning person in a container, this container cannot repeat the same person containter losers=new Containter (n);//Put the loser in a container
for (int i=0;i<n;i++) {Winners.add (Sc.next ());
Losers.add (Sc.next ());
} int count=0;
Boolean flag;
for (int i=0;i<winners.num;i++) {flag=true; for (int j=0;j<losers.num;j++) {if (Winners.contain[i].compareto (Losers.contain[j]) ==0) {//Determine if there is a winner in the container of the loser flag
=false;
Break
}} if (flag) {//If the winning person does not appear in the loser, it can be regarded as the champion count++;
}} if (count==1) {//When the number of people who can be regarded as a champion is 1 o'clock, then the winner System.out.println ("Yes") can be produced;
}else{System.out.println ("No");
}}}} class containter{string[] contain;
int num=0;
public containter (int n) {this.contain=new string[n]; } public void Add (String str) {if (Isexist (str)) {///Determine if a return has already been present in the container;
} contain[num++]=str;
public boolean isexist (String str) {for (int i=0;i<num;i++) {if (Contain[i].compareto (str) ==0) {return true;
}} return false;
}
}
Method Two:
Package search.topology;
Import Java.util.Scanner; public class P2094_2 {public static int num;//statistics public static string[] str;//store name public static int[][] arc;//record who won? Who public static int[] degree;//store the number of times each person loses, that is, the degree of each node into public static void main (string[] args) {Scanner sc=new Scanner (Sy
stem.in);
int n;
while (Sc.hasnext ()) {n=sc.nextint ();
if (n==0) {break;
} int x, y;
num=0;
Arc=new int[1000][1000];
Degree=new int[1000];
Str=new String[n*2];
for (int i=0;i<n;i++) {//Because the string is not indexed, convert the string to int instead of x=isexist (Sc.next ());
Y=isexist (Sc.next ());
Arc[x][y]=1;
degree[y]++;
} toposort ();
}} private static void Toposort () {int count=0;
for (int i=0;i<num;i++) {if (degree[i]==0) {count++;
}} if (Count==1) {System.out.println ("Yes");
}else{System.out.println ("No"); }} private static int isexist (String s) {//If this person is present, return the number of this person, if not, then add this person's serial number for (int i=0;i<num;i++) {if (str[i].c Ompareto (s) ==0) {
return i;
}} str[num++]=s;
return num-1; }
}