产生冠军(用的链表,邻接表暂时不会)

本文介绍了一个算法,用于预测基于乒乓球比赛结果的潜在冠军。通过跟踪每位选手的胜负记录,算法可以确定是否已产生不可争议的胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。
 

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值