http://poj.org/problem?id=3687

看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了。。后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边。

 #include<stdio.h>
#include<string.h>
int map[][];
int n,in[],ans[];
int topo()
{
int i,j;
for (i = n; i >= ; i --)
{
for (j = n; j >= ; j --)
{
if (!in[j])
{
ans[j] = i;
--in[j];
break;
}
}
if (j < ) return ;
for (int k = ; k <= n; k ++)
{
if (map[j][k])
--in[k];
}
}
return ;
}
int main()
{
int m,t,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(map,,sizeof(map));
memset(ans,,sizeof(ans));
memset(in,,sizeof(in));
for (int i = ; i <= m; i ++)
{
scanf("%d%d",&a,&b);
if (!map[b][a])
{
map[b][a] = ;
++in[a];
}
}
if (topo())
{
for (int i = ; i <= n; i ++)
{
if (i==)
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
puts("");
}
else
puts("-1");
}
return ; }

Labeling Balls(拓扑)的更多相关文章

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

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

  2. POJ3687.Labeling Balls 拓扑排序

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

  3. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  4. PKU 3687 Labeling Balls(拓扑排序)

    题目大意:原题链接 给出N个未编号的质量各不相同的球,以及它们质量轻重的大小关系,给它们从1-N贴标签编号,无重复.问是否存在可行的编号方法,不存在输出-1, 如果存在则输出唯一一种方案,此方案是使得 ...

  5. Java实现Labeling Balls(拓扑排序的应用)

    1 问题描述 给出一些球,从1N编号,他们的重量都不相同,也用1N标记加以区分(这里真心恶毒啊,估计很多WA都是因为这里),然后给出一些约束条件,< a , b >要求编号为 a 的球必须 ...

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

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

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

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

  8. Labeling Balls(变种拓扑)

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

  9. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

随机推荐

  1. 获取qq音乐json数据---某课网音乐app学习

    移动端qq音乐地址:https://m.y.qq.com/ .抓取QQ音乐数据 请求首页时,有如下链接,回调了jsonp https://c.y.qq.com/splcloud/fcgi-bin/p. ...

  2. CAD把一个DWG文件中的多个图框一次性全部插入到打开的DWG文件中

    主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块,详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...

  3. 关于panda中dataframe的与&运算*(stackoverflow高票答案)

    85 down vote favorite 31 What explains the difference in behavior of boolean and bitwise operations ...

  4. js 阻止冒泡事件和默认事件

    阻止事件冒泡 window.enent ? window.enent.cancelBubble = true : e.stopPropagation() function stopBubble(eve ...

  5. proxy 跨域配置, 针对有axios的baseURL

    1.首先主要的config文件下的index.js中的proxytable配置 proxyTable:{ '/proxy': { target:'http://192.168.2.141:8080', ...

  6. Git下的标签

    发布一个版本时,我们通常先在版本库中打一个标签(tag),这样,就唯一确定了打标签时刻的版本.将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照 ...

  7. 【Codeforces 478C】Table Decorations

    [链接] 我是链接,点我呀:) [题意] 给你r,g,b三种颜色的气球 每张桌子要放3个气球 但是3个气球的颜色不能全都一样 (允许两个一样,或者全都不一样) 问你最多能装饰多少张桌子 [题解] 先把 ...

  8. everyday two problems / 3.11 - 3.17

    7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...

  9. C++ premier 中文版 学习笔记(第五章 表达式)

    解应用和自增组合使用的理解 由于后自增操作的优先级高于解引用操作,因此 *iter++ 等效于*(iter++).子表达式 iter++ 使 iter 加 1,然后返回 iter 原值的副本作为该表达 ...

  10. cocoa 运行apple脚本文件的方法

    NSString* path = [[NSBundle mainBundle] pathForResource:@"ScriptName" ofType:@"scpt&q ...