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 ...
随机推荐
- 加权平均值 WAvg”
https://wenku.baidu.com/view/13d974fff8c75fbfc77db2d3.html
- GUI初步和frame&panel
java的话这个GUI其实不是什么重点,但我们也要学习,重点是学习这种图形编程的思路. java里面对于图形的一些类都封装在了AWT和它的一些子包里.AWT(抽象窗口开发包) 当 ...
- 在Paint事件中绘制控件(边框)
单纯的自己记录,将来会继续添加,侥幸被大家发现了的话请不要太鄙视... private void panel4_Paint(object sender, PaintEventArgs e) { Con ...
- SpringBoot 2.x (4):配置文件与单元测试
SpringBoot的配置文件有默认的application.properties 还可以使用YAML 区别: application.properties示例: server.port=8090 s ...
- 升级 Cocoapods 到1.2.0指定版本,降低版本及卸载
=====================升级版本=================== CocoaPods 1.1.0+ is required to build SnapKit 3.0.0+. 在 ...
- Android习惯——给全部Activity添加集合管理
title: 2017-3-23 Android习惯--给全部Activity添加集合管理 public void ActivityCollector{ public static List<A ...
- 【数据分析 R语言实战】学习笔记 第三章 数据预处理 (下)
3.3缺失值处理 R中缺失值以NA表示,判断数据是否存在缺失值的函数有两个,最基本的函数是is.na()它可以应用于向量.数据框等多种对象,返回逻辑值. > attach(data) The f ...
- 【PostgreSQL-9.6.3】一般视图
PG视图分为两种,一种是物化视图,一种是一般视图.本篇文章主要写一般视图哪些事儿.所谓一般视图,通俗点说,就是由查询语句定义的虚拟表.视图中的数据可能来自一张或多张表. 1. 视图创建语句 CREAT ...
- BotFramework学习-02
1.请求的Message格式 { "type": "Message", "id": "fd89606f8014453ca5587e ...
- asp 数据库 模块化 - 思路是没一个页面有一个自己的数据类 这里用nPath表示
<!--#include file="db_class.asp" --> <% '当前页面数据 nPath = "..\..\.." 't模块 ...