HDU1285_确定比赛名次

题目大意

有 n 个队伍, 只知道 m 条关于两支队伍之间胜负的关系. 求 排名. 排名不唯一, 此时输出编号较小的队伍的排名. 输入数据保证有一个符合要求的排名.

思路1

最开始想到的是 使用队列进行排序 , 但是 传统地 使用队列, 并不能解决这道问题. 所以需要 理解拓扑排序原理 后, 写一个 O(n^2) 的循环解决这道题.

但是, 此题有一个 可能一组边会被输入多次, 所以每次在入度的数组 inDeg 中, 每次 减小 的值是 edge[loc][j]

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 550
using namespace std;
int inDeg[MAXN];
int edge[MAXN][MAXN];
int main(){
int nVertex, nEdge;
while(scanf("%d%d", &nVertex, &nEdge) != EOF){
memset(inDeg, 0, sizeof(inDeg));
memset(edge, 0, sizeof(edge));
for(int i = 0; i < nEdge; i++){
int a, b;
scanf("%d%d", &a, &b);
edge[a][b]++;
inDeg[b]++;
}
for(int i = 1; i <= nVertex; ++i){
int loc;
for(loc = 1; loc <= nVertex && inDeg[loc] != 0; ++loc);
inDeg[loc]--;
if(i != 1)
printf(" ");
printf("%d", loc); for(int j = 1; j <= nVertex; ++j){
if(edge[loc][j] == 0)
continue;
inDeg[j] -= edge[loc][j];
}
}
printf("\n");
}
return 0;
}

思路2

使用优先队列, 开头将每一个 入度为0 的点压进队列, 因为是 priority_queue<int, vector, greater >, 所以每次就是最小的点在最前, 解决了多个解的问题.

代码

#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#define MAXN 550
using namespace std;
int nVertex, nEdge;
int edge[MAXN][MAXN];
int inDeg[MAXN];
void topOrder(){
priority_queue<int, vector<int>, greater<int> >que;
// queue<int> que;
for(int i = 1; i <= nVertex; i++){
if(inDeg[i] == 0)
que.push(i);
}
int flag = 0;
while(!que.empty()){
int front = que.top();
que.pop(); if(flag) printf(" ");
printf("%d", front);
flag = 1; for(int i = 1; i <= nVertex; i++){
if(edge[front][i] == 0)
continue;
inDeg[i] -= edge[front][i];
if(inDeg[i] == 0)
que.push(i);
}
}
}
int main(){
while(scanf("%d %d", &nVertex, &nEdge) != EOF){
memset(edge, 0, sizeof(edge));
memset(inDeg, 0, sizeof(inDeg));
for(int i = 0; i < nEdge; i++){
int a, b;
scanf("%d %d", &a, &b);
edge[a][b]++;
inDeg[b]++;
}
topOrder();
printf("\n");
}
return 0;
}

HDU1285_确定比赛名次的更多相关文章

  1. hduoj 1285 确定比赛名次

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory ...

  2. HDU 1285 确定比赛名次(简单拓扑排序)

    题目链接: 传送门 确定比赛名次 Time Limit: 1000MS     Memory Limit: 65536K Description 有N个比赛队(1 Input 输入有若干组,每组中的第 ...

  3. ACM: HDU 1285 确定比赛名次 - 拓扑排序

     HDU 1285 确定比赛名次 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  4. HDU 1285 确定比赛名次

    传送门 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. HDU 1285 拓普排序 基本模板例题 确定比赛名次

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. TOJ3651确定比赛名次

    确定比赛名次   Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte Total Submit: 23          ...

  7. (hdu)1285 确定比赛名次

    Problem Description 有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接 ...

  8. hdoj 1285 确定比赛名次【拓扑排序】

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

随机推荐

  1. mysql安装 2018最新安装mysql教程及遇到的问题解决Windows下

    原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/8094659.html 今天因为换了个LINUX系统 把我的E盘不小心给卸载了 结果还是不能用 ...

  2. Linux分区扩容

    lz在MAC上面使用Linux虚拟机,开始只建了一个分区,挂载在”/”目录下.现在硬盘空间不够了,所以lz就来给这个分区扩容. 首先,当然是要给虚拟机分配更多的硬盘空间喽(lz用的是VMware Fu ...

  3. python学习(四)--POST请求

    from urllib import requestimport urllibimport re #网页版有道翻译是ajax的post请求. #浏览器请求地址http://fanyi.youdao.c ...

  4. L1-002 打印沙漏 (20 分)

    L1-002 打印沙漏 (20 分) 方法:清晰思路,纸上写出实例,注意循环使用 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * ** ...

  5. HashMap put、get方法源码分析

    HashMap.java的实现是面试必问的问题. JDK版本 java version "1.8.0_91" Java(TM) SE Runtime Environment (bu ...

  6. js之方法

    原文 在一个对象中绑定函数,称为这个对象的方法. 在JavaScript中,对象的定义是这样的: var xiaoming = { name: '小明', birth: 1990 }; 但是,如果我们 ...

  7. 【基础笔记】《html&CSS设计与构造网站》一书导读

    ◉HTML 1.结构网页使用HTML HyperText Markup Language 来描述页面结构超文本标记语言允许对文本建立链接,允许对文本进行标记网页开头都有一个DOCTYPE 文档类型 声 ...

  8. MySQL查询笔试综合题练习

    题目要求: 在某个数据库下建表: create table stu( -> name char(3) not null default '', -> subject varchar(10) ...

  9. Popularize what is heart of mobile phone?

    From: http://tech.sina.com.cn/mobile/n/2014-09-29/08399657494.shtml 只谈核数没意义 带你重新认识手机SoC 2014年09月29日  ...

  10. 某虚拟定位APP从破解到原理分析

    工具环境ida7.0iphone 6ios 10.2 0x00:基本情况 1. 该app可以修改模拟手机地理位置(gps.基站.WIFI),拥有全局定位.指定应用定位.模拟扫街等功能,只能在已越狱的I ...