排名表

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/problem/show/1150

Description

暑假前集训已经过了一半了,我们将会把当前排名公布出来。但是此刻秋实大哥却心急火燎,因为他不慎把排名删除了。

一共有n个人参加排名,每个人都有一个名次,没有哪两个人的名次是相同的。现在秋实大哥掌握的一些情报,比如Ai的名次要先于Bi。(编号从1开始)

你能帮秋实大哥恢复出排名表吗?

Input

第一行一个数字 T (T≤10),表示测试数据组数

每组测试数据,第一行两个数 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 排名表 拓扑排序的更多相关文章

  1. cdoj 排名表 拓扑排序 排名输出 贪心

    //并不理解为什么需要反向建图,由大到小倒序确定排名.感觉正向由小到大和反向由大到小应该是一样的. 解:拓排+贪心,反向建边,先找排名靠后的(now,不知道为什么) #include<cstdi ...

  2. CDOJ 图论专题 A.不是图论 强连通分量+拓扑排序 经典

    题目链接  在其中纠错第一次wa代码 #include <cstdio> #include <cstring> #include <cstdlib> #includ ...

  3. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  4. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 609  Solved: 318[Submit][Status][Di ...

  6. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  7. 图——拓扑排序(uva10305)

    John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task i ...

  8. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  9. poj 3687(拓扑排序)

    http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...

随机推荐

  1. Android 动画深入解析

    http://blog.csdn.net/rain_butterfly/article/details/39642613

  2. 存储过程中使用事务与try catch

    一.存储过程中使用事务的简单语法 在存储过程中使用事务时非常重要的,使用数据可以保持数据的关联完整性,在Sql server存储过程中使用事务也很简单,用一个例子来说明它的语法格式: 代码 : ) ) ...

  3. 【LeetCode 238】Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  4. 【LeetCode】202 - Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. UI控件入门

    其实整个学习过程.确实有点儿混乱,因为不懂的东西太多,一会儿看这,一会儿看那,马上就乱了. 还是先做点儿简单的事儿,说控件,但不会说完全,只是入个门,知道怎么学控件,具体要学好每一个控件需要项目中磨练 ...

  6. vim开发环境配置

    一.大饱眼福 看了效果图,肯定有人说, 这都有啥功能?就花哨? 告诉你,你说花哨就错了,开玩笑?我们程序猿可都是实打实的人,说谎都不会,咋会忽悠人呢. 下面我来告诉你,这都有些什么功能: 文件索引功能 ...

  7. js遇到这样基础题,看你能不能作对呢

    var a = (function() { return typeof arguments; })(); alert(a); //Object var b = (function(x) { delet ...

  8. c++中获取字符cin,getchar,get,getline的区别

    http://www.imeee.cn/News/GouWu/20090801/221298.html cin.get()与getchar()函数有什么区别? 详细点..C++中几个输入函数的用法和区 ...

  9. eclipse gradle 自动打包

    直接在eclipse项目中建立一个文件,文件名为build.gradle.其实还可以用eclipse再项目上面右击,export->Android->Generate Gradle bui ...

  10. gradle gradlew 的使用

    jcenter() 仓库比 mavenCentral() 仓库快,因此最好将jcenter 放前面,这样下载速度最快. 使用本地软件仓库:repositories { flatDir { dirs ' ...