Labeling Balls POJ - 3687 优先队列 + 反向拓扑
优先队列 + 反向拓扑
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 1e5 + ;
int n, m;
vector<int>G[maxn];
int inDeg[maxn];
int dp[maxn];
int cnt;
int sum ,ans ;
int mp[][];
bool topsort()
{
//queue<int>q;
priority_queue<int>q;
//queue<int>q;
cnt = ;
sum = ;
while(!q.empty())q.pop();
for(int i = ; i <= n ; i++) if(!inDeg[i]) q.push(i);
while(!q.empty())
{
int now = q.top();
q.pop();
// cout << now << endl;
sum ++;
dp[now] = n--;
// cout << dp[now] << endl;
for(int i = ; i < G[now].size(); i++)
{
int to = G[now][i];
inDeg[to]--;
if(!inDeg[to]) q.push(to);
// dp[to] = max(dp[to],dp[now] + G[now][i].second);
} }
if(sum == ans)return ;
else return ;
} int main()
{
int t;
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
// cin >> t;
cin >> t;
while(t--){ cin >> n >> m;
ans = n;
memset(inDeg,,sizeof inDeg);
memset(dp,,sizeof dp);
for(int i = ; i <= n ; i++) G[i].clear();
while(m--)
{
int u, v, w;
cin >> u >> v ; //if(mp[u][v] == 0)
G[v].push_back(u); //G[v].push_back(make_pair((u,w)));
inDeg[u] ++;
}
if(topsort())
//int maxx = 0;
for(int i = ; i <= ans ; i++)
printf("%d%c",dp[i]," \n"[i == ans]);
else
puts("-1");
}
return ;
}
Labeling Balls POJ - 3687 优先队列 + 反向拓扑的更多相关文章
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- HDU 4857 逃生 (优先队列+反向拓扑)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 解题报告:有n个点,有m个条件限制,限制是像这样的,输入a b,表示a必须排在b的前面,如果不 ...
- POJ 3687 逆序拓扑
额.题目大意:有N个球.编号和重量都是唯一不重复的.然后.给你m个pair a和b,表示编号为a的点一定比编号为b的点轻.然后捏.输出每个点对应的重量.关键是要求.如果有多种可能性的话,输出让序号小的 ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16100 Accepted: 4726 D ...
- poj 3687 Labeling Balls【反向拓扑】
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12246 Accepted: 3508 D ...
- POJ 3687:Labeling Balls(优先队列+拓扑排序)
id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...
- POJ3687——Labeling Balls(反向建图+拓扑排序)
Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...
- poj——3687 Labeling Balls
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14835 Accepted: 4346 D ...
随机推荐
- 「SDOI2017」硬币游戏
题目链接 问题分析 首先一个显然的做法就是建出AC自动机,然后高斯消元.但是这样的复杂度是\(O(n^3m^3)\)的. 我们发现其实只需要求AC自动机上\(n\)个状态的概率,而其余的概率是没有用的 ...
- JavaWeb_客户端相对/绝对路径和服务器端路径
客户端的绝对路径和相对路径 相对路径:相对与某个基准目录的路径,在同一根目录下各子目录文件之间的相互引用, 绝对路径:指目录下的绝对位置,直接到的目标位置 @charset "UTF-8&q ...
- 分布式-信息方式- JMS基本概念
JMS基本概念 ■JMs是什么 JMS Java Messag/ Servite,Java消息服务 ...
- 第三周课程总结&实验报告
课程总结 在这周对Java进行了更深层次的学习,Java的学习也变得越来越困难而有趣,加入了一些新的构造新的方法,还学习了一些简化代码的方式. 面向对象的基本概念 对于面向对象的程序设计有三个主要特征 ...
- web.py下获取get参数
比较简单,就直接上代码了: import web urls = ( '/', 'hello' ) app = web.application(urls, globals()) class hello: ...
- 配置Toad链接远程Oracle数据库
当前环境: 本机系统:Win7 64位 Toad版本:11 32位 数据库:Oracle 10g =================================== 与PLSQL Develope ...
- shell历史命令
1.每分钟备份历史命令 制定计划任务:每分钟执行备份历史命令的脚本 注意:要用python写计划任务脚本,因为用shell脚本写的计划任务总是不执行 先写脚本: [root@master ~]# ca ...
- python基础及安装
一.python介绍 介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,Guido开始写能够解释Python语言语法的解释器.Python这个名 ...
- windows系统安装的两个阶段
windows visa .windows7.windows server 2008系统安装的两个阶段1.Boot.WIM----Windows PE系统.提供了windows7安装向导! 2.Ins ...
- PHP 静态变量的介绍
PHP静态变量 变量在程序里是生命周期,存储在栈上,函数执行完就会被释放,而静态变量不同之处在于,函数执行完后并不会被释放,而是保留到下次函数调用,直到程序执行完,才从栈上释放 在PHP中有3类全局变 ...