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 ...
随机推荐
- java中HashMap的keySet()和values()
我们通常说,keySet()返回所有的键,values()返回所有的值,其实是不太对的,因为无论是keySet()和values(),其实都没有实质的内容,且容我慢慢说来. 他们前者返回了一个Set, ...
- poj 1028 Web Navigation
Web Navigation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31088 Accepted: 13933 ...
- 诠释JavaScript中的this
文章首发:http://www.cnblogs.com/sprying/p/3573456.html 使用this的几种场合 1. 执行函数时,判断函数是对象方法还是一个单独的函数?单独的函数this ...
- awk - Unix, Linux Command---reference
http://www.tutorialspoint.com/unix_commands/awk.htm NAME gawk - pattern scanning and processing lang ...
- Shader学习笔记
Shader学习笔记 例子: Shader "SrfShader1"{ //定义显示在Inspector中的变量,并从Inspector中获取值 Properties{ _Colo ...
- Please, configure Web Facet first!idea报这错的解决办法!!
Please, configure Web Facet first!idea报这错的解决办法!! 今天在idea导入用eclipse的项目,然后运行项目的时候报这个错, 看下图 网上找了好多都没解决, ...
- scss-字符串连接符
+ 运算可用于连接字符串: // SCSS p { cursor: e + -resize; } // 编译后的 CSS 样式 p { cursor: e-resize; } 请注意,如果带引号的字符 ...
- SQLAlchemy的使用---查询的更多操作
# 查询更多操作 from create_table import User, engine from sqlalchemy.orm import sessionmaker Session = ses ...
- Zepto结合Swiper的选项卡
我们昨天说了关于Angular的选项卡,那今天就说一下Swiper的选项卡吧! 今天的选项卡是Zepto结合Swiper的选项卡,咱么明天再说纯纯的Swiper的吧! 既然是关于Zepto和Swipe ...
- 闭包中的this
var name="pushline";//全局变量 var obj=new Object(); obj.name="jms"; obj.getName=fun ...