cdoj 1150 排名表 拓扑排序
排名表
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/problem/show/1150
Description
一共有n个人参加排名,每个人都有一个名次,没有哪两个人的名次是相同的。现在秋实大哥掌握的一些情报,比如Ai的名次要先于Bi。(编号从1开始)
你能帮秋实大哥恢复出排名表吗?
Input
每组测试数据,第一行两个数 n(1≤n≤200)和 m(0≤m≤40000),接下来m行,每行两个数a和b(1≤a,b≤N),表示a的名次要先于b
Output
对于每组测试数据,输出一行,从1号到n号每个人的名次。
如果有多个解,让编号为1的人的名次尽量小,然后让编号为2的人的名次尽量小,然后让编号为3的人的名次尽量小......
如果没有解,输出−1
Sample Input
5
4 0
4 1
1 1
4 2
1 2
2 1
4 1
2 1
4 1
3 2
Sample Output
1 2 3 4
-1
-1
2 1 3 4
1 3 2 4
HINT
题意
题解:
逆向拓扑排序,注意,这个是输出每个的排名,而不是按着排名顺序输出人
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int head[maxn];
int top;
int d[maxn];
priority_queue<int>q;
struct edge
{
int v,next;
}e[maxn];
int cnt;
int ans[maxn];
int ans1[maxn];
int n,m;
void insert(int u,int v)
{
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt;
cnt++;
} void solve(int x)
{
q.pop();
ans[++top]=x;
for(int i=head[x];i>=;i=e[i].next)
{
d[e[i].v]--;
if(d[e[i].v]==)
q.push(e[i].v);
}
} int main()
{
//freopen("test.txt","r",stdin);
int t=read();
while(t--)
{
n=read(),m=read();
cnt=top=;
memset(head,-,sizeof(head));
memset(d,,sizeof(d));
for(int i=;i<=m;i++)
{
int u=read(),v=read();
insert(v,u);
d[u]++;
}
for(int i=;i<=n;i++)
if(!d[i])
q.push(i);
while(!q.empty())
{
solve(q.top());
}
if(top!=n)
printf("-1\n");
else
{
int cnt3=;
int first=;
for(int i=n;i;i--)
{
ans1[ans[i]]=cnt3++;
}
for(int i=;i<=n;i++)
{
if(i==)
printf("%d",ans1[i]);
else
printf(" %d",ans1[i]);
}
printf("\n");
}
}
return ;
}
cdoj 1150 排名表 拓扑排序的更多相关文章
- cdoj 排名表 拓扑排序 排名输出 贪心
//并不理解为什么需要反向建图,由大到小倒序确定排名.感觉正向由小到大和反向由大到小应该是一样的. 解:拓排+贪心,反向建边,先找排名靠后的(now,不知道为什么) #include<cstdi ...
- CDOJ 图论专题 A.不是图论 强连通分量+拓扑排序 经典
题目链接 在其中纠错第一次wa代码 #include <cstdio> #include <cstring> #include <cstdlib> #includ ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...
- 图——拓扑排序(uva10305)
John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
随机推荐
- USB鼠标过一段时间后失灵问题的修复
现象: USB鼠标计算机锁屏一段时间后,不能动了,拔下来重新插上后,又恢复正常了. 原因: 这是系统默认USB电源管理造成的.一段时间不用后,自动关闭了USB电源. 解决方法: 1.进入设备管理器 在 ...
- centos7虚拟机无法上网的解决办法
今天在VMware虚拟机中经过千辛万苦终于安装好了centos7..正兴致勃勃的例行yum update 却发现centos系统貌似默认网卡没配置好,反馈无法联网.经过一番研究,终于让centos连上 ...
- 把两个DataTable连接起来,相当于Sql的Inner Join方法
在C#中把两个DataTable连接起来,相当于Sql的Inner Join方法 作者:浪漫十一狼在下面的例子中实现了3个Join方法,其目的是把两个DataTable连接起来,相当于Sql的Inne ...
- IA32系统级架构总览(一) 实模式和保护模式
应用程序的编写大部分的时候是不必关心系统级架构的,最多学习一下平台所给的API即可,也就是我们通常说的黑箱子.但是在学习操作系统的时候,系统级架构是要关心的. 系统级架构很难学习,其中一个很大的原因是 ...
- 2014搜狗前端面经【B事业部】
本来就投了一份简历,后来又收到了个B事业部的面试电话,今天刚面完一面,总体感觉还是很基础的,其中一名面试官帅到不行啊!另一个也不差,真是幸胡...(sorry,跑题了...) 上来先做了份笔试题,超级 ...
- WinDriver&PCIE
1.安装VS2012 安装VS2012略过,主要用它来做数据传输应用程序的,WINDRIVER提供了一系列API接口,方便了用户,使用户能直接进入用户态的编程,因为内核态的编程它已做好,不需要进行修改 ...
- 20151007kaggle Titanic心得
Titanic是kaggle上一个练手的比赛,kaggle平台提供一部分人的特征,以及是否遇难,目的是预测另一部分人是否遇难.目前抽工作之余,断断续续弄了点,成绩为0.79426.在这个比赛过程中,接 ...
- 轻松学Shell之认识正规表达式
离线下载观看:http://down.51cto.com/data/148117 650) this.width=650;" onclick='window.open("htt ...
- 第三百二十一天 how can I 坚持
上班第一天,感觉时间过得好慢. 心里好烦,做什么都没心情,感觉没有勇气了,虽然早上说了那么多,但不敢去面对了. 咋整? <猪老三><野子>. 好想去看<美人鱼> 不 ...
- cocos2dx android版本移植时的Error format not a string literal and no format arguments解决方案
原文地址 : http://www.cnblogs.com/hhuang2012/p/3336911.html cocos2dx android版本移植时的Error format not a str ...