Labeling Balls(poj 3687)
题意:N个球,从1~N编号,质量不同,范围1~N,无重复。给出小球间的质量关系(<), 要求给每个球贴标签,标签表示每个球的质量。按编号输出每个球的标签。如果解不唯一,按编号小的质量小排。
/*
被这道题坑惨了,刚开始读错题了,题目要求输出每个球的重量,而不是按重量输出球的编号
还有,因为题目要求如果有多解,则编号小的质量小,因为拓扑排序是按质量从小到大来找,
所以没有什么好的方法,于是反向拓扑,并且倒着找,把质量大的留给编号大的就可以了。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 210
#define M 40010
using namespace std;
int head[N],out[N],vis[N],a[N],n,m;
struct node
{
int v,pre;
};node e[M];
void add(int i,int x,int y)
{
e[i].v=y;
e[i].pre=head[x];
head[x]=i;
}
void work()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(i,y,x);out[x]++;
}
int tot=n;
while()
{
int k=;
for(int i=n;i>=;i--)
if(!out[i]&&!vis[i])
{
a[i]=tot--;
vis[i]=;
k=i;
break;
}
if(!k)break;
for(int i=head[k];i;i=e[i].pre)
if(out[e[i].v])out[e[i].v]--;
}
if(tot)printf("-1");
else
for(int i=;i<=n;i++)
printf("%d ",a[i]);
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(e,,sizeof(e));
memset(head,,sizeof(head));
memset(out,,sizeof(out));
memset(vis,,sizeof(vis));
work();
}
return ;
}
Labeling Balls(poj 3687)的更多相关文章
- POJ 3687 Labeling Balls (top 排序)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15792 Accepted: 4630 D ...
- POJ 3687 Labeling Balls(拓扑排序)题解
Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them ...
- Codeforces A. Kyoya and Colored Balls(分步组合)
题目描述: Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- POJ 3687:Labeling Balls(优先队列+拓扑排序)
id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...
- Labeling Balls(拓扑排序wa)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12466 Accepted: 3576 D ...
- Labeling Balls(poj3687)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13109 Accepted: 3782 D ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
随机推荐
- TRUNCATE TABLE 与 DELETE (转)
TRUNCATE TABLE 删除表中的所有行,而不记录单个行删除操作.TRUNCATE TABLE 与没有 WHERE 子句的 DELETE 语句类似:但是,TRUNCATE TABLE 速度更快, ...
- JAVA Android王牌教程
Java基础 在Java基础系列文章中,我将说明Java的基础内容,特别是面向对象的相关概念. Java基础01 从HelloWorld到面向对象 Java基础02 方法与数据成员 Java基础03 ...
- 173 Binary Search Tree Iterator 二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器.调用 next() 将返回二叉搜索树中的下一个最小的数.注意: next() 和hasNext() 操作的时间复杂度是O(1),并使用 ...
- Android Studio中找出不再使用的资源
顶部Analyze菜单中选择Run Inspection by Name 在弹出的输入框中输入unused resources
- SQLite busy handler
SQLite doesn't support high concurrency. In case of a lot of concurrent access from multi-process or ...
- schtasks /create 计划任务 中文路径 名字都要加“” 子命令 /tn /tr 前面要空格 否则会出错
echo off echo. 清空连接 net use * /del /y echo. 连接 net use \\192.168.1.2\人力资源部\考勤\考勤数据-小莫提供 "密码&quo ...
- (转)淘淘商城系列——zookeeper单机版安装
http://blog.csdn.net/yerenyuan_pku/article/details/72717744 这篇文章,我单独来说一下zookeeper如何安装.在实际开发中,zookeep ...
- new Buffer 生成二进制数据
node编辑环境下: > new Buffer("admin")<Buffer 61 64 6d 69 6e> 通过post请求,服务端接收到是流数据,必须把流数 ...
- Duplicate fragment name ERROR Jetty Maven Plugin
http://stackoverflow.com/questions/5802096/duplicate-fragment-name-error-jetty-maven-plugin 4down vo ...
- Parker Gear Pump - Gear Pump Seal Is More O-Ring: Role
Parker Gear Pump introduction Gear pump lip seal is mainly used in reciprocating dynamic seals. C ...