拓扑排序简单题

产生冠军

题目传送:HDU - 2094 - 产生冠军

思路:因为只要确定是否产生冠军,根据题意可以得知当且仅当入度为0的只有一个时可以产生冠军,可以用map来映射存储的人的名字

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

map<string, int> mp;
int deg[1005];
int cnt;
int n;

int main() {
    while(scanf("%d", &n) != EOF) {
        if(n == 0) break;
        memset(deg, 0, sizeof(deg));
        cnt = 1;
        mp.clear();

        string s1, s2;
        for(int i = 0; i < n; i ++) {
            cin >> s1 >> s2;
//          cout << s1 << " " << s2 << endl;
            if(mp.find(s1) == mp.end()) {
                mp[s1] = cnt ++;
            }
            if(mp.find(s2) == mp.end()) {
                mp[s2] = cnt ++;
            }
            int t1 = mp[s1];
            int t2 = mp[s2];
            deg[mp[s2]] ++;
        }

        int sum = 0;
        for(int i = 1; i < cnt; i ++) {
            if(deg[i] == 0) {
                sum ++;
            }
        }

        if(sum == 1) {
            printf("Yes\n");
        }
        else printf("No\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-12 18:50:15

拓扑排序简单题的相关文章

题目1450:产生冠军(拓扑排序简单题)

题目链接:http://ac.jobdu.com/problem.php?pid=1450 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1450 产生冠军.cpp // Jobdu // // Created by PengFei_Zheng on 21/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio

拓扑排序 --- 模板题

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10536    Accepted Submission(s): 4120 Problem Description 有 N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能

Poj 1094 拓扑排序 水题

Sad..这么水的题WA了无数发,题目要看仔细啊,留下来做个警告把 #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #i

hdu--1811--并查集&amp;&amp;拓扑排序&lt;好题&gt;

做了这题 绝逼 累啊.. mle -- re<stack overflow>--tle--wa---ac 经过这么5步 终于AC了 这题 我觉得可以让你更好地来 理解 拓扑排序的一些细节问题 首先 这题 为什么要用到并查集呢? 因为 会有 A = B这种情况的出现 然后可能再来个 B =C A = D....那么我们就需要将它们全部表示成一个点 那么就是都用一个根结点来表示 然后 这边 是要判断 能不能根据给出的条件 形成一个排列 那么就是个 拓扑问题 根据 > <情况来判断 我觉

UVa 10305 - Ordering Tasks (拓扑排序裸题)

John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input The input will consist of several instances of the problem. Each instance begins with

HDU 1285 确定比赛名次(拓扑排序基础题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目: 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前.现在请你编程序确定排名. Input 输入有若干组,每组中的第一行为二个数N(1<=N<=500),M:其中N表示队

HDU 1285 确定比赛名次 拓扑排序水题

Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前.现在请你编程序确定排名. Input 输入有若干组,每组中的第一行为二个数N(1<=N<=500),M:其中N表示队伍的个数,M表示接着有M行的输入数据.接下来的M行数据中,每行也有两个整

HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题】

Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5906    Accepted Submission(s): 2734 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is

HDU 4857 逃生 拓扑排序好题 第一次做CLJ出的题

逃生 Problem Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有的富.1号最富,2号第二富,以此类推.有钱人就贿赂负责人,所以他们有一些好处. 负责人现在可以安排大家排队的顺序,由于收了好处,所以他要让1号尽量靠前,如果此时还有多种情况,就再让2号尽量靠前,如果还有多种情况,就让3号尽量靠前,以此类推. 那么你就要安排大