Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16032   Accepted: 4713

Description

Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that:

  1. No two balls share the same label.
  2. The labeling satisfies several constrains like "The ball labeled with a is lighter than the one labeled with b".

Can you help windy to find a solution?

Input

The first line of input is the number of test case. The first line of each test case contains two integers, N (1 ≤ N ≤ 200) and M (0 ≤ M ≤ 40,000). The next M line each contain two integers a and bindicating the ball labeled with a must be lighter than the one labeled with b. (1 ≤ a, b ≤ N) There is a blank line before each test case.

Output

For each test case output on a single line the balls' weights from label 1 to label N. If several solutions exist, you should output the one with the smallest weight for label 1, then with the smallest weight for label 2, then with the smallest weight for label 3 and so on... If no solution exists, output -1 instead.

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

Source

题意:

给定N个球,编号分别是1-N,重量也是1-N,任意两个球的编号和重量不相等。

给定一些约束:类似编号为a的球比编号为b的球轻,要求输出的答案必须让编号为1的球重量尽量轻,接着是编号为2的球重量尽量轻,一直到编号为N的球尽量轻。

思路:

原命题:编号小的球重量尽量轻。

逆否命题:重量大的球编号尽量大,逆向拓扑排序即可。

PS:数据存在重边,自环等。。

代码:

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e5 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff; int n,m,cnt;
bool mp[][];
int du[N],head[N];
int a[];
struct P{int to,nxt;}e[N];
void add(int u,int v){
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void topsort()
{
int k=n;
priority_queue<int>q;
for(int i=;i<=n;i++) if(!du[i]) q.push(i);
while(!q.empty())
{
int tmp=q.top();
q.pop();
a[tmp]=k--;
for(int i=head[tmp];~i;i=e[i].nxt){
int v=e[i].to;
du[v]--;
if(!du[v]) q.push(v);
}
}
if(k!=) printf("-1\n");
else
{
for(int i=;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
}
}
void init()
{
memset(mp,,sizeof(mp));
memset(a,,sizeof(a));
memset(head,-,sizeof(head));
memset(du,,sizeof(du));
cnt=;
}
int main()
{
int t;
ci(t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
if(mp[x][y]==) continue;
mp[x][y]=,add(y,x);//反向边
du[x]++;
}
topsort();
}
return ;
}

POJ3687 反向拓扑排序的更多相关文章

  1. CF-825E Minimal Labels 反向拓扑排序

    http://codeforces.com/contest/825/problem/E 一道裸的拓扑排序题.为什么需要反向拓扑排序呢?因为一条大下标指向小下标的边可能会导致小下标更晚分配到号码,导致字 ...

  2. 逃生(HDU4857 + 反向拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...

  3. HDU-4857-逃生-反向拓扑排序+优先队列

    HDU-4857 题意就是做一个符合条件的排序,用到拓扑序列. 我一开始wa了多发,才发现有几个样例过不了,发现1->2->3...的顺序无法保证. 后来就想用并查集强连,还是wa: 后来 ...

  4. 正向与反向拓扑排序的区别(hdu 1285 确定比赛名次和hdu 4857 逃生)

    确定比赛名次 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  5. 【HDOJ4857】【反向拓扑排序】

    http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  6. HDU 2647 Reward【反向拓扑排序】

    Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. hdu 4857(好题,反向拓扑排序)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  8. HDU 4857 逃生 (反向拓扑排序 & 容器实现)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 逃生 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  9. hdoj 2647 Reward【反向拓扑排序】

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. mysql导入导出csv

    LOAD DATA local INFILE '/tmp/stb.csv' INTO TABLE stb FIELDS TERMINATED BY ',' enclosed by '"' l ...

  2. SQL Server ->> CLR存储过程枚举目录文件并返回结果集

    因工作需要写了个CLR存储过程枚举目录文件并返回结果集 using System; using System.IO; using System.Collections.Generic; using S ...

  3. koa2获取用户ip

    调用下面方法即可获取 // koa2 中 req 为 ctx.req const getUserIp = (req) => { return req.headers['x-forwarded-f ...

  4. 长大Tips的第一步

    任务进度:登陆界面的初步设计. 运行环境:windows10 编译环境:netbeans 编写语言:java 界面展示: 任务简介: 本次任务指示简单的完成了界面设计,登陆按钮暂未实现,持续更新中,敬 ...

  5. Android(java)学习笔记4:线程的控制

    1. 线程休眠: Java中线程休眠指让正在运行的线程暂停执行一段时间,进入阻塞状态,通过调用Thread类的静态方法sleep得以实现. 当线程调用sleep进入阻塞状态后,在其休眠的时间内,该线程 ...

  6. PHP语言开发微信公众平台(订阅号)之注册(1)

    1.百度搜索"微信公众平台" 2.选择微信公众平台官网并单击打开 3.进入官网页面,单击 "立即注册" 进入注册页面 4.进入注册页面,单击订阅号 5.进入订阅 ...

  7. 如何在vue2.0项目中引用element-ui和echart.js

    1 项目中怎样添加elment-ui 和 echart.js 1.1直接在packjson 里面的 dependencies 配置 "element-ui": "^1.3 ...

  8. 【luogu P2863 [USACO06JAN]牛的舞会The Cow Prom】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2863 求强连通分量大小>自己单个点的 #include <stack> #include ...

  9. Entity Framework5.0运行时错误ObjectStateManager 中已存在具有同一键的对象

    EF写了个简单的框架,在把查询出来的数据修改回去时,报了ObjectStateManager 中已存在具有同一键的对象这样一个错误,寻寻觅觅终于找到了最终的解决方案. ObjectStateManag ...

  10. js 防抖 节流 JavaScript

    实际工作中,通过监听某些事件,如scroll事件检测滚动位置,根据滚动位置显示返回顶部按钮:如resize事件,对某些自适应页面调整DOM的渲染:如keyup事件,监听文字输入并调用接口进行模糊匹配等 ...