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 ...
随机推荐
- Webapi备忘-20140109
r01的翻译系列:http://www.cnblogs.com/r01cn/category/428069.html 接收FROM数据 Webapi接收表单数据,如果有多个数据比如x=1&y= ...
- HBase的索引
LSM树由来.设计思想以及应用到HBase的索引 讲LSM树之前,需要提下三种基本的存储引擎,这样才能清楚LSM树的由来: 哈希存储引擎 是哈希表的持久化实现,支持增.删.改以及随机读取操作,但 ...
- WPF开发的FTP文件上传工具
前言 最近楼主在改版的项目里有使用到FTP上传文件,所以看到之前的项目里FTP上传的功能,然后抽个时间学习一番,做着做着就作出了这个工具了(虽然有些验证的功能没加上),但是基本的上传功能还是很好用的, ...
- Orchard学习
Data Source=.\SQLEXPRESS;Initial Catalog=Arale;Integrated Security=True;
- 设置控件Enable=false,控件颜色不变
[System.Runtime.InteropServices.DllImport("user32.dll ")] public static extern int SetWind ...
- PHP gbk转换成utf8
/** * GBK ASCII 转换成utf8 */ public function to_utf8($str){ $detect = array('ASCII', 'GBK', 'UTF-8'); ...
- ios缩放图片
http://blog.csdn.net/yanfangjin/article/details/7456681
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- Redis:在windows环境安装Redis
Redis:在windows环境安装Redis 第一步: 下载windows版本的Redis:https://github.com/MSOpenTech/Redis. 第二步: 在命令行执行:D:\r ...
- scribefire 多博客管理利器 安装详解
scribefire 多博客管理利器 安装详解 一.ScribeFire介绍 ScribeFire 是 Firefox (火狐浏览器)上著名的博客写作工具,目前已跨平台支持多浏览器(Firefox,C ...