前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >HDOJ 1285 确定比赛名次(拓扑排序)

HDOJ 1285 确定比赛名次(拓扑排序)

作者头像
谙忆
发布于 2021-01-21 03:03:04
发布于 2021-01-21 03:03:04
36000
代码可运行
举报
文章被收录于专栏:程序编程之旅程序编程之旅
运行总次数:0
代码可运行

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行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。

Output 给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。

其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。

Sample Input 4 3 1 2 2 3 4 3

Sample Output 1 2 4 3

拓扑排序介绍: 拓扑排序(Topological Order)是指,将一个有向无环图(Directed Acyclic Graph简称DAG)进行排序进而得到一个有序的线性序列。

这样说,可能理解起来比较抽象。下面通过简单的例子进行说明! 例如,一个项目包括A、B、C、D四个子部分来完成,并且A依赖于B和D,C依赖于D。现在要制定一个计划,写出A、B、C、D的执行顺序。这时,就可以利用到拓扑排序,它就是用来确定事物发生的顺序的。

在拓扑排序中,如果存在一条从顶点A到顶点B的路径,那么在排序结果中B出现在A的后面。

拓扑排序(适用于有向无环图): 1)选一个入度为0的点p输出; 2)从图中删除p点 3)将p全部后继点的入度-1 4)重复1-3,直到全部点都输出

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            //n个队伍,m行数据
            int map[][] = new int[505][505];
            int times[] = new int[505];

            //初始化
            for(int i=0;i<n;i++)
                times[i]=0;
            for(int i=0;i<m;i++){
                for(int j=0;j<m;j++){
                    map[i][j]=0;
                }
            }

            int a;
            int b;
            for(int i=0;i<m;i++){
                a = sc.nextInt();
                b = sc.nextInt();
                if(map[a-1][b-1]==0){//避免重复边多次加1
                    map[a-1][b-1]=1;
                    times[b-1]++;
                }

            }

//          for(int i=0;i<n;i++){
//              for(int j=0;j<n;j++){
//                  System.out.print(map[i][j]+" ");
//              }
//              System.out.println("----------");
//          }
//          
//          for(int i=0;i<n;i++){
//              System.out.print(times[i]+" ");
//          }

            //拓扑排序
            int count =0;
            int x=0;
            while(count<n){//表示入栈的n个元素全部排完了

                //找一个入度为0的点  x
                x=0;
                while(times[x]!=0){
                    x++;
                }
                times[x]=-1;
                    //避免再次统计,改0为-1
                    //System.out.print(x+" ");

                //将x的后继节点入度减1
                for(int i=0;i<n;i++){
                    if(map[x][i]==1){
                        times[i]--;
                    }
                }
                count++;
                if(count<n){
                    System.out.print((x+1)+" ");
                }else{//最后一个输出后面没有空格
                    System.out.print(x+1);
                }
            }
            System.out.println();
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/03/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
hduoj------确定比赛名次
拓扑排序结合代码的完整理解 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9284    Accepted Submission(s): 3613 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依
Gxjun
2018/03/22
6340
图论--拓扑排序--HDU-1285确定比赛名次
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。
风骨散人Chiam
2020/11/05
4480
暑假集训之专题----拓扑排序题解
第一单: Problem A Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 26   Accepted Submission(s) : 5 Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmoni
Gxjun
2018/03/22
6390
比赛名次
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
喜欢ctrl的cxk
2019/11/08
4410
HDU2544-dijstla-填坑
我反手一个好家伙,查了很多遍,写的没问题啊,最后才发现min设置最大值的时候,设置的Integer.MAX_VALUE,设置这个就是错,所以就看了以前的设置的是0x3f3f3f3f
知识浅谈
2022/06/22
2030
HDOJ 3466 Proud Merchants
Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any m
谙忆
2021/01/21
2410
HDOJ 3466 Proud Merchants
HDOJ(HDU) 1785 You Are All Excellent(角度运算)
Problem Description 本次集训队共有30多人参加,毫无疑问,你们都是很优秀的,但是由于参赛名额有限,只能选拔部分队员参加省赛。从学校的角度,总是希望选拔出最优秀的18人组成6支队伍来代表学校。但是,大家也知道,要想做到完全客观,是一件很难的事情。因为选拔的标准本身就很难统一。 为了解决这个难题,我现在把问题作了简化,现在假设每个队员都是二维平面中的一个点,用(xi,yi)坐标来表示,一个队员的能力可以用他到原点的欧几里德距离来表示。由于这种排名标准太~客观了,新队员很难有出头的机会,很多人很是郁闷。特别是一个废话不是很多、不是特别暴躁、号称盖帽高手的伪**就很有意见,他现在要求改革排名规则,并且自己提出了一套号称绝对公正的方案: 现在不是用一个点来表示一个队员了,而是用原点到该队员所在的点所构成的向量来表示一个队员。如果该向量和X正轴夹角比较小的话,就说他的能力比较高,排名就应该靠前。 这就是著名的“伪氏规则”(说实话,这规则我有点怀疑其客观性,因为我知道他的坐标是(3.1,0.1)…)
谙忆
2021/01/21
2350
HDOJ/HDU 2550 百步穿杨(注意排序)
Problem Description 时维九月,序属三秋,辽军大举进攻MCA山,战场上两军正交锋.辽军统帅是名噪一时的耶律-James,而MCA方则是派出了传统武将中草药123.双方经过协商,约定在十一月八日正午十分进行射箭对攻战.中草药123早早就开始准备,但是他是武将而不是铁匠,造弓箭的活就交给聪明能干的你了,现在告诉你每种弓箭规格,即箭身的长度,以及每种规格弓箭所需要的数目,要求你把需要的弓箭都输出. 弓箭的基本样子为 “>+—+>”,其中”+—+”为箭身,数据保证箭身长度 > 2
谙忆
2021/01/21
2250
HDOJ 1236 排名(练耐心题)
Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的 考生,并将他们的成绩按降序打印。
谙忆
2021/01/21
1970
HDOJ 1040 As Easy As A+B
Problem Description These days, I am thinking about a question, how can I get a problem as easy as
谙忆
2021/01/20
2980
HDOJ/HDU 1984 Mispelling4(删除第n个字符~)
Problem Description Misspelling is an art form that students seem to excel at. Write a program that removes the nth character from an input string.
谙忆
2021/01/21
3210
HDOJ(HDU) 2523 SORT AGAIN(推导排序、、)
Problem Description 给你N个整数,x1,x2…xn,任取两个整数组合得到|xi-xj|,(0 < i,j<=N,i!=j)。 现在请你计算第K大的组合数是哪个(一个组合数为第K大是指有K-1个不同的组合数小于它)。
谙忆
2021/01/21
2700
HDOJ(HDU) 1708 Fibonacci String
Problem Description After little Jim learned Fibonacci Number in the class , he was very interest in it. Now he is thinking about a new thing – Fibonacci String .
谙忆
2021/01/21
2200
HDOJ 2037 今年暑假不AC
Problem Description “今年暑假不AC?” “是的。” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%…”
谙忆
2021/01/20
3550
HDOJ 2037 今年暑假不AC
HDOJ/HDU 1015 Safecracker(深搜)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === “The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. Fortunately old Brumbaugh from research knew Klein’s secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. Five of the uppercase letters form the combination that opens the safe. By combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.) To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, …, Z=26). The combination is then vwxyz. If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary.”
谙忆
2021/01/21
3600
HDOJ 2015 偶数求和
Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。
谙忆
2021/01/20
4110
HDOJ/HDU 1982 Kaitou Kid - The Phantom Thief (1)(字符串处理)
Problem Description Do you know Kaitou Kid? In the legend, Kaitou Kid is a master of disguise, and
谙忆
2021/01/21
3070
HDOJ/HDU 1982 Kaitou Kid - The Phantom Thief (1)(字符串处理)
HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description Goldbach’s Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
谙忆
2021/01/21
3530
HDOJ 2094 产生冠军
Problem Description 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛。 球赛的规则如下: 如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C。 如果A打败了B,B又打败了C,而且,C又打败了A,那么A、B、C三者都不可能成为冠军。 根据这个规则,无需循环较量,或许就能确定冠军。你的任务就是面对一群比赛选手,在经过了若干场撕杀之后,确定是否已经实际上产生了冠军。
谙忆
2021/01/20
3440
确定比赛名次(拓扑排序) - HDU 1285
这次先讲理论,因为拓扑排序在日常工作中用的并不多,甚至于很多人可能忘了计算机中存在这样一种排序。我大概的整理一下拓扑排序的定义和应用,以便看了这题之后,对拓扑排序有一个比较深的印象。
ACM算法日常
2018/08/07
1.2K0
确定比赛名次(拓扑排序) - HDU 1285
相关推荐
hduoj------确定比赛名次
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文