zoukankan      html  css  js  c++  java
  • HDU 2094 产生冠军 (图论)

    题面

    Problem 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

    思路

    这道题可能看上去会很想那道踢足球的拓扑排序题目,但是如果你自己去举几个例子,你会发现,如果存在这样一个冠军,那么这必定会是一条链的情况,一直这样下去,才会链的根点有可能是冠军,如果存在环,或者或者这条链断开了,那么就不存在冠军了。所以我们这个问题就成了判断每个点的入度,如果有且只有一个点的入度为0,所以就判断存在冠军,否则就不存在。这道题的话,需要使用map映射一下字符串和int之间的关系,方便我们处理。

    代码实现

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <list>
    #include <map>
    #include <queue>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <numeric>
    #include<vector>
    #include <functional>
    #define x first
    #define y second 
    using namespace std;
    typedef long long ll;
    const int maxn=500005;
    typedef vector <int> vec;
    const int mod=100000;
    int in_dg[2010];
    int num;
    int main () {
       int n;
        while (cin>>n) {
            if (n==0) break;
            num=0;
            map<string, int> ma;
            string temp1,temp2;
            for (int i=1;i<=2000;i++) {
                in_dg[i]=0;
            }
            for (int i=1;i<=n;i++) {
                cin>>temp1>>temp2;
                if (ma[temp1]==0) ma[temp1]=++num;
                if (ma[temp2]==0) ma[temp2]=++num;
                in_dg[ma[temp2]]++;
            }
            int ans=0;                                                                                                                                                                                  
            for (int i=1;i<=num;i++) if (in_dg[i]==0) ans++;
            if (ans==1) cout<<"Yes"<<endl;
            else cout<<"No"<<endl; 
        }
        return 0;
        }
    
    
  • 相关阅读:
    Object-c学习之路七(oc字符串操作)
    Object-c学习之路六(oc字符串文件读写)
    Object-c学习之路五(@protocol协议)
    jQ效果(滑动)
    jQ效果(淡入淡出)
    jQ效果(显示隐藏)
    jQ笔记2
    jq笔记
    DOM节点操作
    两个css样式
  • 原文地址:https://www.cnblogs.com/hhlya/p/13193075.html
Copyright © 2011-2022 走看看