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 ...
随机推荐
- repoquery详解——linux查看包依赖关系的神器
repoquery是yum扩展工具包yum-utils中的一个工具,所有如果你没有repoquery命令的话,可以先 sudo yum install yum-utils 安装yum-utils包.是 ...
- DIV水平垂直居中的CSS兼容写法
DIV水平垂直居中,非IE浏览器可以用CSS3来处理,IE浏览器中分别处理IE6和/IE7.IE8.IE9. 在IE低版本中,虽然大致上没有问题,但还是有一些细微的显示问题. 示例如下: <!D ...
- centos6.5下搭建oracle 11g
1.安装依赖 sudo yum install binutils compat-libstdc++-33 compat-libstdc++-33.i686 elfutils-libelf elfuti ...
- 数据降维(Dimensionality reduction)
数据降维(Dimensionality reduction) 应用范围 无监督学习 图片压缩(需要的时候在还原回来) 数据压缩 数据可视化 数据压缩(Data Compression) 将高维的数据转 ...
- Spring课程 Spring入门篇 5-3 配置切入点 pointcut
1 解析 1.1 xml常见的配置切入点写法 2 代码演练 2.1 xml配置切入点 1 解析 1.1 xml常见的配置切入点写法 2 代码演练 2.1 xml配置切入点 xml配置: <? ...
- Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题
1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...
- 【Android】8.0活动的生命周期(一)——理论知识、活动的启动方式
1.0 Android是使用任务(Task)来管理活动的,活动就像栈一样堆放着在一起. 每个活动的生命周期最多可能会有四种状态: 1.1 运行状态 位于栈顶 1.2 暂停状态 不在栈顶但在界面上仍处于 ...
- 跨平台移动开发_PhoneGap 再次点击返回键切换到桌面效果
PhoneGap 再次点击返回键切换到桌面效果 相关代码 <!DOCTYPE html> <html> <head> <title> PhoneGap ...
- greenplum导数据
Greenplum and Hadoop HDFS integration Step 1: Install Java on greenplum Step 2: Set JAVA and HADOOP ...
- haproxy学习——简介、基本配置(二)
官网:http://www.haproxy.org/ 个人感觉haproxy学习的重点在于配置上,把配置文档搞懂了就明白大部分套路了.不过本篇内容属于入门学习:1.使用haproxy简单的实现负载均衡 ...