HDU1285_确定比赛名次
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_确定比赛名次的更多相关文章
- hduoj 1285 确定比赛名次
http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDU 1285 确定比赛名次(简单拓扑排序)
题目链接: 传送门 确定比赛名次 Time Limit: 1000MS Memory Limit: 65536K Description 有N个比赛队(1 Input 输入有若干组,每组中的第 ...
- ACM: HDU 1285 确定比赛名次 - 拓扑排序
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- HDU 1285 确定比赛名次
传送门 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 1285 拓普排序 基本模板例题 确定比赛名次
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- TOJ3651确定比赛名次
确定比赛名次 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte Total Submit: 23 ...
- (hdu)1285 确定比赛名次
Problem Description 有N个比赛队(<=N<=),编号依次为1,,,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接 ...
- hdoj 1285 确定比赛名次【拓扑排序】
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
随机推荐
- android 学习资源网址
脚本之家: http://www.jb51.net/list/list_233_2.htm csdn: http://blog.csdn.net/xubo578/article/details/571 ...
- Linux 下安装 Memcached 和 PHP 开启 Memcached 扩展 及 LAMP 环境的安装
http://blog.csdn.net/liruxing1715/article/details/8269563
- PHP邮件发送
php带有内置的mail() 发送邮件函数,但是较为繁琐:建议上网下载一个PHPMailer:
- 【Ubuntu】ubuntu 16.04 设置root用户初始密码
安装ubuntu成功后,都是普通用户权限,并没有最高root权限,如果需要使用root权限的时候,通常都会在命令前面加上 sudo . 我们一般使用su命令来直接切换到root用户的,但是如果没有给r ...
- 上下文(Context)和作用域(Scope)
函数的每次调用都有与之紧密相关的作用域和上下文.从根本上来说,作用域是基于函数的,而上下文是基于对象的. 换句话说,作用域涉及到所被调用函数中的变量访问,并且不同的调用场景是不一样的.上下文始终是th ...
- asp.net之cookie
1.创建cookie HttpCookie userCookie = new HttpCookie("userInfo"); userCookie["name" ...
- 2017年10月9日 冒泡&去重复习
今天看了一下,就是数组跟js还是不太熟悉 冒泡排序 var arr = [4, 2, 1, 3, 6, 5]; for(var i = 1; i < arr.length; ...
- Java web service 异常
1.org/apache/commons/discovery/tools/DiscoverSingleton Exception in thread "main" java.lan ...
- C#简单实现读取txt文本文件并分页存储到数组
最近做一个VR项目,需要把某个中草药的介绍信息分页显示到unity场景里然后用VR手柄切换信息. unity的脚本是c#,就先在本地写了个代码测试了一下,利用控制台测试输出,到时候拷贝函数过去再结合交 ...
- css居中那些事
一.css垂直居中 1.line-height(适用于单行文本居中) eg: html:<p class="wordp">123</p>- css: .w ...