码迷,mamicode.com
首页 > 其他好文 > 详细

九度 题目1450:产生冠军

时间:2014-12-17 09:11:32      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:九度   图论   题目1450产生冠军   

题目描述:

有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。
球赛的规则如下:
如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。
如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。
根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。

输入:

输入含有一些选手群,每群选手都以一个整数n(n<1000)开头,后跟n对选手的比赛结果,比赛结果以一对选手名字(中间隔一空格)表示,前者战胜后者。如果n为0,则表示输入结束。

输出:

对于每个选手群,若你判断出产生了冠军,则在一行中输出“Yes”,否则在一行中输出“No”。

样例输入:
3
Alice Bob
Smith John
Alice Smith
5
a c
c d
d e
b e
a d
0

样例输出:

Yes

No

思路:如果所有顶点中只有一个顶点的入度为零则yes;否则no

代码如下:

#include <stdio.h>
#include <string.h>
#define N 1001

struct Node {
    char name[100];
    int Indegree;
}nodes[N];

int main(){
    int n;
    while(scanf("%d",&n) != EOF && n != 0){
        char str1[100];
        char str2[100];
        int count = 0;
        for(int i = 0; i < n; i++){
            scanf("%s%s",str1,str2);
            int f1 = 0,f2 = 0;//f1代表胜者,f2代表输者
            for(int j = 0; j < count; j++){
                if(strcmp(str1,nodes[j].name) == 0){
                    f1 = 1;
                }
                if(strcmp(str2,nodes[j].name) == 0){
                    nodes[j].Indegree++;
                    f2 = 1;
                   // break;
                }
            }
            if(f1 == 0){//不存在该选手
                strcpy(nodes[count].name,str1);//保存该选手信息
                nodes[count++].Indegree = 0;
            }
            if(f2 == 0){
                strcpy(nodes[count].name,str2);
                nodes[count++].Indegree = 1;
            }
        }
        int ans = 0;
        for(int i = 0; i < count; i++){
            if(nodes[i].Indegree == 0){
                ans++;
            }
        }
        if(ans == 1){
            printf("Yes\n");
        }else{
            printf("No\n");
        }
    }
}

九度 题目1450:产生冠军

标签:九度   图论   题目1450产生冠军   

原文地址:http://blog.csdn.net/j754379117/article/details/41972381

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!