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的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
随机推荐
- Codeforces 667B Coat of Anticubism
链接:传送门 题意:题目balabala说了一大堆,然而并没什么卵用,给你n个数,将这个集合分割成两部分,构成三角形的两个边,让你求补充的那个边最短是多长 思路:三角形三边具有 a + b > ...
- [学习笔记] CS131 Computer Vision: Foundations and Applications:Lecture 3 线性代数初步
向量和矩阵 什么是矩阵/向量? Vectors and matrix are just collections of ordered numbers that represent something: ...
- 在Vue中使用了Swiper ,动态从后台获取数据的之后,swiper滑动失效??
在Vue中使用了Swiper ,动态从后台获取数据的之后,swiper滑动失效?? 是因为swiper提前初始化了,那时候数据还没有完全出来.这里有两种解决办法 1. 使用vue提供的$nextTic ...
- cogs 2056. 无平方因子数
2056. 无平方因子数 ★☆ 输入文件:non.in 输出文件:non.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 给出正整数n,m,区间[n,m]内的无 ...
- android:px,dp(dip),sp的差别
1.px:表示屏幕的实际像素,比如320*480的屏幕在横向有320个像素,在纵向有480个像素,假设指定的某个空间的单位为px.那么在不同分辨率下的手机上.显示的都是指定的大小.一般不推荐使用px. ...
- HDU Train Problem I (STL_栈)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- jQuery中focusin()和focus()、find()和children()的差别
jQuery中focus()和focusin().focus()和children()的差别 focus()和focusin() focus()和focusin()的差别在于focusin()支持事件 ...
- 剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到)
剪切具有CornerRadius的RectangleGeometry(可能在Ripple中用到) 1.新建Converter public class BorderClipConverter : IM ...
- bzoj2464: 中山市选[2009]小明的游戏(最短路)
2464: 中山市选[2009]小明的游戏 题目:传送门 题解: 最短路的裸题... 代码: #include<cstdio> #include<cstring> #inclu ...
- 如何用Android studio生成正式签名的APK文件
必须签名之后才可以发布到app商店中. 平时的调试的app都有默认的签名. 下面是生成带签名的APK的步骤: 1. Build 选择 Generate Signed APK 2. 弹出框,第一次选择C ...