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 ...
随机推荐
- 1、IO概述及File类
- NGUI利用深度测试实现新手引导遮罩
实现原理:实际上就是先利用渲染队列渲染,然后再利用ZTest,改变渲染的遮挡关系. PS:Depth Testing:深度测试,也叫深度缓冲.只有最靠近观察者的物体会被绘制.深度即Z,该值越小表示离观 ...
- opencv实现人脸,人眼,微笑检测
1.首先实现人脸检测 import cv2 img = cv2.imread("people.jpg",1) #读入图像 #导入人脸级联分类器引擎,“.xml”文件里包含了训练出来 ...
- 经典问题——输出n对括号的所有组合
问题 n对括号有多少种合法的组合,比如两对括号可以有两种:()()和(()) 思路 问题等价为:在一个字符串中包含两种字符:'('和')',他们出现的次数都为n,并且任何时候'('出现的次数总是大于或 ...
- 【转】Web 前沿——HTML5 Form Data 对象的使用
XMLHttpRequest Level 2 添加了一个新的接口——FormData.利用 FormData 对象,我们可以通过 JavaScript 用一些键值对来模拟一系列表单控件,我们还可以使用 ...
- formSelects-v4.js 基于Layui的多选解决方案
https://hnzzmsf.github.io/example/example_v4.html
- 使用nsight调试caffe
首先你需要下载caffe源码,然后先编译好,注意一定要将Makefile.config里的DEBUG := 1注释掉 可以看到注释掉debug后编译会生成的.build_debug目录,调试过程中需要 ...
- 网络爬虫之框架(Scrapy)
Scrapy爬虫框架 爬虫框架是实现爬虫功能的一个软件结构和功能组件集合. 爬虫框架是一个半成品,能够帮助用户实现专业网络爬虫. Scrapy爬虫框架结构:
- 关闭 将jar或者aar发布到到mvn 中(用github作为仓库), 通过gradle dependency 方式集成
使用Android Studio开发的用户,都希望通过maven远程仓库的方式来集成jar.aar文件,但是这些文件时如何发布的呢? 通常开发者都会将jar文件发布到sonatype上,以提供给其他开 ...
- vim要粘贴的话,先set paste,然后粘贴,然后再set nopaste
要粘贴的话,先set paste,然后粘贴,然后再set nopaste