B - 产生冠军
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。
Input
输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。
Output
对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[20];
int c;
struct node *next;
};
void main()
{
int i,j,m,n,f;
char b[20],a[20];
struct node *head,*p,*r,*q;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
while(~scanf("%d%*c",&n)&&n)
{
while(n--)
{
f=1;
scanf("%s%s",a,b);
for(r=head->next;r;r=r->next)
{
if(strcmp(r->name,a)==0)
f=0;
if(strcmp(r->name,b)==0)
r->c=1;
}
if(f)
{
p=(struct node*)malloc(sizeof(struct node));
p->next=head->next;
head->next=p;
strcpy(p->name,a);
p->c=0;
}
p=(struct node*)malloc(sizeof(struct node));
p->next=head->next;
head->next=p;
strcpy(p->name,b);
p->c=1;
}
f=0;
for(p=head->next;p;p=p->next)
if(p->c==0)f++;
if(f==1)printf("Yes\n");
else printf("No\n");
while(head->next)
{
r=head->next;
head->next=r->next;
free(r);
}
}
}