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

题意:有N个重量1到N的点,把这N个点涂色,要求在一定的约束下颜色a必须比颜色b要轻,如果有多种选择则让重量最小的对应编号1,然后剩下中重量最小的给编号2,一次类推

题解:逆向建图,这样取出来的就是最后选择的点,并标上最大重量,把邻接点入度为0的加入到优先队列中,然后取编号最大的,为的是使得留着编号最小的给重量最大的

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
int edge[maxn][maxn];
int cnt[maxn];
int ans[maxn];
int tot;
int toporder(int n)
{
priority_queue <int> q;
int i,j,k;
for(i=;i<=n;i++)
if(cnt[i] == ) q.push(i);
for(i=;i<=n;i++)
if(q.empty()) return ;
else
{
j = q.top();q.pop();
ans[j] = tot--;
for(k=;k<=n;k++)
if(edge[j][k] && (--cnt[k]) == ) q.push(k);
}
return ;
}
int main ()
{
int n,m,t,i,u,v;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(edge,,sizeof(edge));
memset(cnt,,sizeof(cnt));
tot = n;
for(i=;i<m;i++)
{
scanf("%d%d",&u,&v);
if(edge[v][u] == ) cnt[u]++;
edge[v][u] = ;
}
if(toporder(n))
{
for(i=;i<=n;i++)
if(i == n)printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
else printf("-1\n");
}
return ;
}

Labeling Balls的更多相关文章

  1. POJ 3687 Labeling Balls()

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

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

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

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

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

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

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

  5. Labeling Balls(拓扑排序wa)

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

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

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

  7. Labeling Balls(变种拓扑)

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

  8. POJ 3687 Labeling Balls (top 排序)

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

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

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

  10. POJ3687.Labeling Balls 拓扑排序

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

随机推荐

  1. CentOS6.5解决中文乱码与设置字符集

    [ CleverCode发表在csdn博客中的原创作品,请勿转载,原创地址:http://blog.csdn.net/clevercode/article/details/46377577] 1)说明 ...

  2. [Redux] Refactoring the Entry Point

    We will learn how to better separate the code in the entry point to prepare it for adding the router ...

  3. [RxJS] Filtering operators: takeLast, last

    Operators take(), skip(), and first() all refer to values emitted in the beginning of an Observable ...

  4. 数缘社区上对libtom的介绍,贴过来先

    http://blog.csdn.net/songlingrebecca/article/details/5879154 基于Bit位运算的C语言库分析及其应用 1 LibTomproject的简要介 ...

  5. Java基础知识强化之集合框架笔记14:List集合存储字符串并遍历

    1. List集合存储学生对象并遍历: 需求:存储字符串并遍历 分析: (1)创建集合对象 (2)创建字符串对象 (3)添加字符串对象到集合中 (4)遍历集合 2. 代码示例: package cn. ...

  6. CUDA与VS2013安装

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  7. 原创翻译:iOS 应用程序 蓝牙后台运行

    默认情况下,普通的CoreBluetooth 任务中的大多数,无论是Central还是peripheral ,在后台或者挂起状况下都是无法进行的.也就是说,你可以通过宣布你的应用程序支持后台处理模式来 ...

  8. Linux软件安装,RPM与YUM

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3843955.html ...

  9. 10.23 noip模拟试题

    尼玛蛋pdf好难粘 直接写了 T1 /*开始写wa了 我真弱2333 关于p的排序规则不只是差值 为了字典序最小 还要拍别的*/ #include<cstdio> #include< ...

  10. Html5新增的语义化标签(部分)

    2014年10月29日,万维网联盟宣布,经过接近8年的艰苦努力,html5的标准规范终于制定完成.这是互联网的一次重大变革,这也许是一个时代的来临! 总结一些h5新增的语义化标签,记录下来方便自己学习 ...