Labeling Balls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12273   Accepted: 3516

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:

  1. No two balls share the same label.
  2. 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 这是一个拓扑题,还不是普通的拓扑,这必须要反向建图+逆向输出,并且注意,这个题让输出的是各个人的位置!!!
 #include <iostream>
#include<cstdio>
#include<cstring> using namespace std;
int map[][];
int degree[];
void topo(int n)
{
int i,j,mark,que[];
for(i=n;i>=;i--)
{
int x=;//标记看是否满足要求
for(j=n;j>=;j--)
{
if(degree[j]==)
{
x=;
mark=j;
break;
}
}
if(x==)
break;
que[mark]=i;
degree[mark]=-;
for(j=;j<=n;j++)
{
if(map[mark][j])
degree[j]--;
}
}
if(i!=-)
printf("-1\n");
else
{
printf("%d",que[]);
for(i=;i<=n;i++)
printf(" %d",que[i]);
printf("\n"); }
} int main()
{
int N,i,m,n,a,b;
scanf("%d",&N);
while(N--)
{
memset(map,,sizeof(map));
memset(degree,,sizeof(degree));
scanf("%d%d",&n,&m);
for(i=;i<m;i++)
{
scanf("%d%d",&a,&b);
if(!map[b][a])//避免重复录入
{
map[b][a]=;
degree[a]++;//反向建图
}
}
topo(n);
}
return ;
}

Labeling Balls--poj3687的更多相关文章

  1. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  2. POJ3687.Labeling Balls 拓扑排序

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13201 Accepted: 3811 Descr ...

  3. Labeling Balls(poj3687)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13109   Accepted: 3782 D ...

  4. POJ 3687 Labeling Balls()

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...

  5. Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...

  6. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  7. Labeling Balls(拓扑排序wa)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12466   Accepted: 3576 D ...

  8. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  9. Labeling Balls(变种拓扑)

    Labeling Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  10. POJ 3687 Labeling Balls (top 排序)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15792   Accepted: 4630 D ...

随机推荐

  1. 转载的别人的ajax跨域解决方法

    http://dynamic.vip.xxxxxx.com/active/<controllers>/<active>/<id> 放在浏览器地址栏中访问可以得到正确 ...

  2. repo sync 时的自动续接脚本[转]

    按理说在repo init  ....之后使用repo sync就可以开始下载源码了,但是在下载过程中经常会出现没网速“死”的情况.当然,我修改了/etc/hosts文件之后就再也么有死过.在没网速提 ...

  3. 怎么通过网站优化来增强SEO效果?

    真正好的网站优化不应该针对哪一个搜索引擎,而应该关注用户的需求.搜索引擎把这这用户指标权重提升,实际上也是希望把更多的精力集中在用户体验的提升上面.那么,这些用户指标应该怎样提升呢?方法多种多样,下面 ...

  4. 10g中注意谓词过滤的位置

    在10g中当主查询的谓词信息,被错误的放入子查询中,会导致子查询无法展开 explain plan for UPDATE DWF.F_PTY_INDIV O SET END_DT = TO_DATE( ...

  5. 【转】NPAPI 插件无法在 Chrome 42 版及更高版本上正常运行

    原文网址:https://support.google.com/chrome/answer/6213033 NPAPI 插件无法在 Chrome 42 版及更高版本上正常运行 您可以利用插件在浏览器中 ...

  6. 【转】Excel快捷键大全

    原文网址:http://www.bm8.com.cn/keyboard/excel.asp 显示和使用"Office 助手"注意 若要执行以下操作,"Microsoft ...

  7. 【转】Service Intent must be explicit的解决方法

    原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...

  8. x2engine

    x2engine 各版本下载 https://bitnami.com/stack/x2crm/installer https://bitnami.com/redirect/to/36211/bitna ...

  9. VS2012 中使用Emacs布局

    微软的反开源行为导致它不断的衰落,问题是还不反省. 下面这篇文章介绍了如何安装emacs布局的插件: http://marxistprogrammer.blog.163.com/blog/static ...

  10. 集成Dubbo服务(Spring)

    Dubbo是什么? Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点. Dubbo[]是 ...