Labeling Balls(变种拓扑)
Labeling Balls
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 20 Accepted Submission(s) : 10
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?
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.
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.
4 0
4 1
1 1
4 2
1 2
2 1
4 1
2 1
4 1
3 2
-1
-1
2 1 3 4
1 3 2 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 205
int InD[];
int Edge[][];
void MakeSet(int Len)
{
int i;
for(i=;i<=Len;i++)
InD[i]=;
return;
}
int ToPoSort(int n,int* ret)
{
int i,j,k;
for(j=n;j>=;j--)
{
for(i=n;i>=;i--)
if(InD[i]==)
{
InD[i]--;
ret[i]=j;
for(k=;k<=n;k++)
if(Edge[i][k]==)
InD[k]--;
break;
}
if(i<)break;
}
if(j<=)
return ;
else
return ;
} int main()
{
int N,M,i,a,b,ID[],T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
MakeSet(N);
memset(Edge,,sizeof(Edge));
memset(ID,,sizeof(ID));
for(i=;i<M;i++)
{
scanf("%d%d",&a,&b);
if(Edge[b][a]==)
{
Edge[b][a]=;
InD[a]++;
}
}
if(ToPoSort(N,ID))
{
for(i=;i<=N;i++)
{
printf("%d",ID[i]);
if(i!=N)putchar();
}
putchar();
}
else
printf("-1\n");
}
return ;
}
修改:2015.2.28(邻接表)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
using namespace std;
#define MAX 215
int InD[MAX];/*InD[i]记录点i的入度*/
int First[MAX];/*First[i]头结点的第一条边的编号*/
struct edge
{
int TO;/*点*/
int Next;/*下一条边的编号*/
}ID[MAX*MAX]; /*边表,无向图的边数记得多弄些*/
int SIGN; /*链表的边数,链表的边数=无向图边数*2=有向图边数*/
void Add_E(int x,int y)/*添加点+更新入度操作*/
{
ID[SIGN].TO=y;
InD[y]++;
ID[SIGN].Next=First[x];
First[x]=SIGN++;
}
int Jude(int x,int y)/*查找与X是否与Y相连,是0,否1*/
{
int i;
for(i=First[x];i!=;i=ID[i].Next) //查找与该点相关的点
{
if(ID[i].TO==y)return ;
}
return ;
}
int ToPoSort(int N,int Num[])/*能够排序返回1,否则返回0*/
{/*N为点的个数(1~N),Num[]用来存储排序后的结果*/
int i,j,k;
for(j=N;j>=;j--)/*修改点一: j作为重量,重量从大到小依次赋值*/
{
for(i=N;i>=;i--)/*修改点二: i作为编号,编号从大到小查找*/
{
if(InD[i]==)/*找到符合的编号。进行赋值*/
{
InD[i]--;
Num[i]=j;/*修改点三: j作为重量,i作为编号(位置)*/
for(k=First[i];k!=;k=ID[k].Next)
{
InD[ID[k].TO]--;
}
break;
}
}
if(i<)break;/*如果全部找完,没有找到,退出*/
}
if(j<=)return ;/*如果重量能够全部赋值,既可行*/
else return ; /*反正不可行。*/
}
int main()
{
int M,N,i,T;
int Num[MAX];
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&M);
int a,b;
for(i=;i<=N;i++){First[i]=;InD[i]=;}
for(i=,SIGN=;i<M;i++)
{
scanf("%d%d",&a,&b);
if(Jude(b,a)) /*判断重边*/
{
Add_E(b,a);
}
}
if(ToPoSort(N,Num))
{
for(i=;i<=N;i++)
{
if(i!=)putchar();
printf("%d",Num[i]);
}putchar();
}
else printf("-1\n");
}
return ;
} /*
4
5 4
1 4
4 2
5 3
3 2 5 3
1 4
4 2
3 5 5 4
5 1
4 2
1 3
2 3 10 5
4 1
8 1
7 8
4 1
2 8
ans:
1 5 3 4 2
1 3 4 2 5
2 4 5 3 1 逆向建图
5 1 6 2 7 8 3 4 9 10 没有判重边的话就输出 -1 */
Labeling Balls(变种拓扑)的更多相关文章
- Labeling Balls(拓扑排序wa)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12466 Accepted: 3576 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(拓扑排序)题解
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
- POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)
题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...
- POJ 3687 Labeling Balls【拓扑排序 优先队列】
题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...
- POJ-3687 Labeling Balls(拓扑)
不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...
- POJ - 3687 Labeling Balls (拓扑)
(点击此处查看原题) 题意 此处有n盏灯,编号为1~n,每盏灯的亮度都是唯一的,且在1~n范围之间,现已知m对灯之间的关系:a b ,说明灯a的亮度比灯b小,求出每盏灯的亮度,要求字典序最小(编号小的 ...
- POJ3687 Labeling Balls(拓扑)
题目链接. 题目大意: N个球,从1-N编号,质量不同,范围1-N,无重复.给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量.按编号输出每个球的标签.如果解不唯一,按编号小 ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
随机推荐
- D5
今天的题目跟前几天的比起来简单了许多 由于T1没有开long long 所以T1全部WA掉了...只悲催的A了第二题 T1:多重背包 其实这一题我真心不会,DP各种弱,简直欲哭无泪... 不过认真的看 ...
- PHP GD 库 缩略图 添加水印
class cls_image { var $error_no = 0; var $error_msg = ''; //var $images_dir = IMAGE_DIR; //var $data ...
- [转]How WebKit Loads a Web Page
ref:https://www.webkit.org/blog/1188/how-webkit-loads-a-web-page/ Before WebKit can render a web pag ...
- ios7学习之路六(隐藏状态栏 )
方法一(代码设置): 现在ios7已经更改为透明,并且不占用屏幕高度.其中隐藏及显示的方法如下: 在uiviewcontroller的子类下,调用: if ([self respondsTo ...
- 冒泡动画按钮的简单实现(使用CSS3)
冒泡动画按钮的简单实现(使用CSS3) 原始的参考文章是 http://tutorialzine.com/2010/10/css3-animated-bubble-buttons/ ,基本原理是利用了 ...
- SQL Server 如何读写数据
01. SQL Server 如何读写数据 一. 数据读写流程简要SQL Server作为一个关系型数据库,自然也维持了事务的ACID特性,数据库的读写冲突由事务隔离级别控制.无论有没有显示开启事 ...
- 一个吊丝android个人开发者的逆袭之路
转眼间,一年多过去了,记得我开发第一款android应用的时候,那是在前年的冬天,我本人是做java的,android的学习和开发完全是业余爱好,从前年上半年到前年下半年大约花了半年的业余时间把and ...
- Citrix 服务器虚拟化之六 Xenserver虚拟机创建与快照
Citrix 服务器虚拟化之六 Xenserver虚拟机创建与快照 在Xenserver上可以创建Windows和Linux等虚拟机,Xenserver支持大部分的主流操作系统,可以使用 XenCe ...
- VC 编程ANSI环境下读写Unicode文件
没有注意到文件编码的不同会产生这么多的问题,在动手以前查询了很多资料,在本博客中收藏了不少先辈的成果,在这里一并表示致敬! 关于ANSI和Unicode编码的原理在这里也不说了,主要讲下如 ...
- asp.net MVC 路由系统
ASP.NET的路由系统是基于物理文件的路由注册,通过调用System.Routing.RouteTable的Routes(RouteCollection)属性的MapPageRoute()方法来完成 ...