HDU.1285 确定比赛名次 (拓扑排序 TopSort)
HDU.1285 确定比赛名次 (拓扑排序 TopSort)
题意分析
裸的拓扑排序
详解请移步
算法学习 拓扑排序(TopSort)
只不过这道的额外要求是,输出字典序最小的那组解。那么解决方案就是每次扫描1-n节点的入度,并且只取出1个编号最小的节点,处理他然后继续取,直到所有节点取出,即可满足字典序最小。
代码总览
#include <iostream>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define nmax 505
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int indegree[nmax];
vector<int> v[nmax];
int n,m;
queue<int> ans;
bool suc = true;bool isfirst = true;
void output()
{
while(!ans.empty()){
int t = ans.front(); ans.pop();
if(isfirst){printf("%d",t); isfirst =false;}
else printf(" %d",t);
}
//printf("\n");
}
void topsort()
{
queue<int> q;
while(1){
for(int i = 1; i<=n;++i){
if(indegree[i] == 0){
q.push(i);
ans.push(i);
indegree[i] = -1;
break;
}
}
if(q.empty()) break;
while(!q.empty()){
int t = q.front();q.pop();
for(int i = 0; i<v[t].size();++i){
int tt = v[t][i];
if(indegree[tt] == -1){
suc =false;
break;
}else
indegree[tt] --;
}
v[t].clear();
if(!suc) break;
}
if(!suc) break;
output();
}
if(suc && ans.size() != n) suc = false;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m) != EOF){
MEM(indegree); suc = true;isfirst = true;
for(int i = 0; i<m; ++i){
int a,b;
scanf("%d%d",&a,&b);
indegree[b]++;
v[a].push_back(b);
}
topsort();
printf("\n");
}
return 0;
}
HDU.1285 确定比赛名次 (拓扑排序 TopSort)的更多相关文章
- ACM: HDU 1285 确定比赛名次 - 拓扑排序
HDU 1285 确定比赛名次 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- hdu 1285 确定比赛名次 拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...
- HDU 1285 确定比赛名次 拓扑排序模板题
http://acm.hdu.edu.cn/showproblem.php?pid=1285 #include <cstdio> #include <cstdlib> #inc ...
- hdu 1285 确定比赛名次 (拓扑)
确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDOJ 1285 确定比赛名次(拓扑排序)
Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委 ...
- [ACM] hdu 1285 确定比赛 (拓扑排序)
确定比赛 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU.3342 Legal or Not (拓扑排序 TopSort)
HDU.3342 Legal or Not (拓扑排序 TopSort) 题意分析 裸的拓扑排序 根据是否成环来判断是否合法 详解请移步 算法学习 拓扑排序(TopSort) 代码总览 #includ ...
- 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)
确定比赛名次 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1285 确定比赛名次(拓扑排序模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意:有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行 ...
随机推荐
- JQuery.extend扩展实现同步post请求
有时需要在jQuery中实现同步post请求,而jquery自带的是异步,需要通过JQuery.extend扩展. 支持ie和firefox,方法转载而来.需要在submit前将form.append ...
- Qt 计算两个日前间隔天数
某一个大神写的 改写了一点 请无视注释 //时间计算法则 /********************************************************************** ...
- ntp-redhat 同步时间配置
1. 选作一个机器作为ntp 服务端,例如 ip 为192.168.0.1 1)安装 ntp服务 yum install ntp 2) 修改ntp.conf 文件 vi /etc/ntp.conf 注 ...
- FFM
转载自http://blog.csdn.net/jediael_lu/ https://blog.csdn.net/jediael_lu/article/details/77772565 点击率预估算 ...
- oracle常用函数总结
Oracle常用函数总结 ---oracle常用函数-----一.数值型常用函数----取整数--select floor(10.1) from dual;--将n四舍五入,保留小数点后m位(默认情况 ...
- DWORD WORD到INT的转换
最近在做一个有关TCP/TP通信的消息解析,涉及到了这方面的转换,记录一下. 首先,如果是在网络传输.消息解析的情况下,要注意一下网络传送使用的是大端还是小端模式,这影响到我们的高低位的传输顺序. W ...
- 由作业题引发对C++引用的一些思考
首先分析一段代码: #include <bits/c++config.h> #include <ostream> #include <iostream> #incl ...
- vim 删除文件全部内容
很多时候我们需要删除脚本文件全部内容, 重新再写入新的内容,进行其他的操作: 很多时候我们对应用程序的排错需要查看日志文件,然而日志中通常有许多我们以前的应用程序产生的日志,其他的日志过多的时候,有时 ...
- mysqlslap工具测试mysql DB的性能
mysqlslap的一个主要工作场景就是对数据库服务器做基准测试. 测试方法 1.测试工具:mysqlslap,mysqlslap是MySQL5.1.4之后自带的benchmark基准测试工具 ...
- ManagementClass("Win32_Share")之共享目录
public class ShareFolder { private static readonly Dictionary<uint, string> ReturnDetails = ne ...