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 ...
随机推荐
- c#二进制、十进制、16进制之间的转换
//十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...
- 2013.7.19 STL库的学习
STL提供了一组表示容器,迭代器,函数对象和算法的模板. 容器是一个与数组类似的单元,可以存储若干个值.容器是同质的,即存储的值的类型一样. 算法是完成特定任务的处方. 迭代器能够用来遍历容器的对象, ...
- Bootstrap相关优质项目推荐
Bootstrap 编码规范by @mdo Bootstrap 编码规范:编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范. jQuery API 中文手册 根据最新的 jQuery 1.1 ...
- jQuery选择表格中的列
以选择table中的第一列为例: 方式1: $("table tr").find("td:first").html("first column&quo ...
- Build FFmpeg for iOS
FFmpeg Build Instructions MAC 10.8 or better Copy ffmpeg-2.0.tar.bz2 (https://ffmpeg.org/releases/ff ...
- Linux环境进程间通信(二):信号(下)
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- socket网络编程快速上手(二)——细节问题(1)
三.细节问题一个也不能少 Socket编程说简单也简单,程序很容易就能跑起来,说麻烦还真是麻烦,程序动不动就出问题.记得刚开始写网络代码的时候,那真是令人抓狂的经历,问题一个套一个,一会服务器起不来了 ...
- Weka开发[4]-特征选择
特征选择,我对这一部分也不熟,大概讲一下,用AttributeSelection进行特征选择,它需要设置3个方面,第一:对属性评价的类(自己到Weka软件里看一下,英文Attribute Evalua ...
- 2013年全球IT公司市值排行榜
开源的应用快速开发平台 排名 公司 市值 国家 荣誉 1 苹果 5006.1 美国 全球市值最大的公司 2 谷歌 2324.4 美国 全球最伟大的互联网公司 3 三星 2290.7 韩国 全球最大的智 ...
- Java笔记:抽象类、接口
这篇笔记主要是抽象类和接口,还有简单介绍下三种设计模式:模板模式.工厂模式.命令模式 1.抽象方法和抽象类(1)抽象方法和抽象类都必须使用abstract修饰符来定义,包含抽象方法的类只能被定义成抽象 ...