杭电 1285 确定比赛名次
来源:互联网 发布:淘宝网二手市场 编辑:程序博客网 时间:2024/05/19 05:02
昨天下午开始看的拓扑排序,今天上午又看了一上午,总算明白了。这道题就是个非常基本的拓扑排序,而且还非常的明显,综合来说,就是个水题了。主要是有两个易错的地方,首先,注意有重边:其次,题目让按从小到大的顺序输出,我是用了set过的,貌似优先队列也可以过。如果没这样的要求,用stack就可以的,运行了31ms,主要时间就是浪费到了set上了。题目:
确定比赛名次
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4262 Accepted Submission(s): 1596
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 31 22 34 3
Sample Output
1 2 4 3
#include <iostream>#include <cstdio>#include <stack>#include <algorithm>#include <set>using namespace std;const int N=510;int map[N][N];int Indegree[N];int teamn;void Topologicsort(){stack<int> S;set<int> SS;set<int>::iterator b;int count=0; for(int i=teamn;i>=1;--i){ if(!Indegree[i]) SS.insert(i);}while(!SS.empty()){ b=SS.begin(); int x=*b; count++; if(count==teamn) printf("%d",x); else printf("%d ",x); SS.erase(b); for(int i=1;i<=teamn;++i){ if(map[x][i]==1){ if(!(--Indegree[i]))SS.insert(i); } }}}int main(){//freopen("4.txt","r",stdin); int m; while(~scanf("%d%d",&teamn,&m)){ memset(map,0,sizeof(map));memset(Indegree,0,sizeof(Indegree));int a,b;for(int i=1;i<=m;++i){ scanf("%d%d",&a,&b); if(!map[a][b]){ map[a][b]=1; Indegree[b]++; }}if(teamn==1){printf("1\n");continue;}Topologicsort();getchar();printf("\n"); } return 0;}
- 杭电 1285 确定比赛名次
- 杭电1285确定比赛名次
- 杭电1285确定比赛名次
- 杭电-1285确定比赛名次(topo)
- 【杭电】[1285]确定比赛名次
- 【杭电1285】确定比赛名次
- 杭电1285确定比赛名次
- 杭电ACM 1285 确定比赛名次(拓扑排序)
- 杭电acm oj 1285 确定比赛名次
- 杭电 1285 确定比赛名次(拓扑排序)
- 杭电1285——确定比赛名次
- 确定比赛名次 HDU杭电1285 【拓扑排序】
- 杭电 1285 确定比赛名次 【拓扑排序】
- 杭电 1285 确定比赛名次(拓扑排序)
- 拓扑排序杭电 1285确定比赛名次
- 杭电1285 确定比赛名次(拓扑排序)
- 杭电-1285 确定比赛名次(模板)
- 1285确定比赛名次
- 加密算法介绍及如何选择加密算法
- 对与object-c 的点表示法
- WCF Learning(6):在Winform Application中调用Duplex Service出现TimeoutException的原因和解决方案
- httpd.conf目录
- vc 部分外挂相关代码,不断增加ing
- 杭电 1285 确定比赛名次
- list_entry()宏
- android之Animation使用方法二
- pv3d入门及贴图
- c# Invoke和BeginInvoke 区别
- 绝好的MongoDB学习资料之一. Database
- Oil Deposits
- 启动Android模拟器报 PANIC: Could not open /.android/avd/test.ini 问题解决
- 锯齿