POJ——T 3687 Labeling Balls
http://poj.org/problem?id=3687
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14842 | Accepted: 4349 |
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:
- No two balls share the same label.
- 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 b indicating 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
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int M();
const int N();
int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} priority_queue<int>que;
int rd[N],ans[N],cnt;
inline void init()
{
sumedge=cnt=;
memset(rd,,sizeof(rd));
memset(ans,,sizeof(ans));
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
} int AC()
{
int t; scanf("%d",&t);
for(int n,m,if_;t--;init())
{
scanf("%d%d",&n,&m);
for(int u,v;m--;ins(v,u))
scanf("%d%d",&u,&v),rd[u]++;
for(int i=;i<=n;i++)
if(!rd[i]) que.push(i);
for(int u,v,weight=n;!que.empty();)
{
u=que.top(); que.pop();
ans[u]=weight--;cnt++;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(--rd[v]==) que.push(v);
}
}
if(cnt!=n) puts("-1");
else
{
for(int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
}
return ;
} int I_want_AC=AC();
int main(){;}
POJ——T 3687 Labeling Balls的更多相关文章
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 3687 Labeling Balls - 贪心 - 拓扑排序
Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ 3687 Labeling Balls()
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- poj——3687 Labeling Balls
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
随机推荐
- UVA10082-WERTYU(紫书例题3.2)
A common typing error is to place the hands on the keyboard one row to the right of the correct posi ...
- BZOJ 2820 luogu 2257 yy的gcd (莫比乌斯反演)
题目大意:求$gcd(i,j)==k,i\in[1,n],j\in[1,m] ,k\in prime,n,m<=10^{7}$的有序数对个数,不超过10^{4}次询问 莫比乌斯反演入门题 为方便 ...
- CF449D Jzzhu and Numbers (状压DP+容斥)
题目大意: 给出一个长度为n的序列,构造出一个序列使得它们的位与和为0,求方案数 也就是从序列里面选出一个非空子集使这些数按位与起来为0. 看了好久才明白题解在干嘛,我们先要表示出两两组合位与和为0的 ...
- [luogu2059 JLOI2013] 卡牌游戏 (概率dp)
题目描述 N个人坐成一圈玩游戏.一开始我们把所有玩家按顺时针从1到N编号.首先第一回合是玩家1作为庄家.每个回合庄家都会随机(即按相等的概率)从卡牌堆里选择一张卡片,假设卡片上的数字为X,则庄家首先把 ...
- ansible 主机清单 /etc/ansible/hosts
主机清单 [webservers] ansible01 ansible02 ansible03 ansible04 [root@ftp:/root] > ansible webservers - ...
- 【Python 学习】通过while循环和for循环猜测年龄
Python中可以通过while和for循环来限制猜测年龄的次数 1. 在猜测年龄的小程序中,不加循环的代码是这样的: age_of_yu = 23 guess_age = int(input(&qu ...
- 新手学python-Day3-模块
模块就是引入别人写的,官方写的工具库,就像扳手,钳子,电锯
- UVALive 5412 Street Directions
Street Directions Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. ...
- Methods and systems for sharing common job information
Apparatus and methods are provided for utilizing a plurality of processing units. A method comprises ...
- bzoj3626【LNOI2014】LCA
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1266 Solved: 448 [Submit][Stat ...