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 ...
随机推荐
- Acwing:137. 雪花雪花雪花(Hash表)
有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,…,ai,6ai,1,ai,2,…,ai,6. 因为雪花的形状是封闭的环形,所以 ...
- Socket编程-基础使用
最后更新:2019-10-25 一 基本概念 socket, 又称为"套接字"或者"插座". 是操作系统提供的一种进程间通信机制.目前大多用于不同网络设备之间的 ...
- Android_(传感器)获取手机中的传感器
传感器是一种检测装置,能够感受被测量的信息,并能将检测和感受到的信息按一定规律变换成电信号或其它所需形式的信息输出 Android操作系统中内置了很多的传感器(物理装置),能够探测.感受外界的信号.物 ...
- MqttConnectReturnCode枚举值
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- maven国内镜像、国内外仓库(直接下载jar)
阿里: https://maven.aliyun.com/mvn/search 官方: http://repo.maven.apache.org/maven2/ maven仓库 阿里巴巴的镜像仓库, ...
- 第七周学习总结&JAVA实验五报告。
JAVA实验报告五: 实验四 类的继承 实验目的 理解抽象类与接口的使用: 了解包的作用,掌握包的设计方法. 实验要求 掌握使用抽象类的方法. 掌握使用系统接口的技术和创建自定义接口的方法. 了解 J ...
- 190707Python-RabbitMQ
一.简单的RabbitMQ示例 生产者 # Author:Li Dongfei import pika connection = pika.BlockingConnection( pika.Conne ...
- 过滤器修改response
过滤器通过doFilter方法的第二个参数ServletResponse将输出发送给客户,但servletResponse参数没有为过滤器提供servlet或jsp页面的访问:执行doFilter方法 ...
- webDriver各版本对应
chromeDriver http://npm.taobao.org/mirrors/chromedriver/ http://chromedriver.storage.googleapis.com/ ...
- layui学习地址
--layui学习地址 ,相当之好用,非常感谢为我们工作和学习提供方便的才子们,谢谢~https://www.layui.com/demo/layim.html