优先队列 + 反向拓扑

//#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 优先队列 + 反向拓扑的更多相关文章

  1. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  2. HDU 4857 逃生 (优先队列+反向拓扑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 解题报告:有n个点,有m个条件限制,限制是像这样的,输入a  b,表示a必须排在b的前面,如果不 ...

  3. POJ 3687 逆序拓扑

    额.题目大意:有N个球.编号和重量都是唯一不重复的.然后.给你m个pair a和b,表示编号为a的点一定比编号为b的点轻.然后捏.输出每个点对应的重量.关键是要求.如果有多种可能性的话,输出让序号小的 ...

  4. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  5. POJ 3687 Labeling Balls(反向拓扑+贪心思想!!!非常棒的一道题)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16100   Accepted: 4726 D ...

  6. poj 3687 Labeling Balls【反向拓扑】

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12246   Accepted: 3508 D ...

  7. POJ 3687:Labeling Balls(优先队列+拓扑排序)

    id=3687">Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10178 Acc ...

  8. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  9. poj——3687 Labeling Balls

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14835   Accepted: 4346 D ...

随机推荐

  1. Vue组件使用

    一.组件概念 有html模板,有css样式,有js逻辑的集合体 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的 ...

  2. happens-before规则和as-if-serial语义

    JSR-133使用happens-before的概念来阐述操作之间的内存可见性.在JMM中,如果一个操作执行的结果需要对另一个操作可见, 那么这2个操作之间必须要存在happens-before关系. ...

  3. Codeforces 343D Water Tree & 树链剖分教程

    原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题 ...

  4. 9.Python关键字(保留字)一览表

    保留字是 Python 语言中一些已经被赋予特定意义的单词,这就要求开发者在开发程序时,不能用这些保留字作为标识符给变量.函数.类.模板以及其他对象命名. Python 包含的保留字可以执行如下命令进 ...

  5. HTML jQuery 文档操作 - html() 方法

    jQuery 文档操作 - html() 方法 jQuery 文档操作参考手册 实例 设置所有 p 元素的内容: $(".btn1").click(function(){ $(&q ...

  6. 并发编程--Concurrent-工具类介绍

    并发编程--Concurrent-工具类介绍 并发编程--Concurrent-工具类介绍 CountDownLatch CylicBarrier Semaphore Condition 对象监视器下 ...

  7. 关于MySQL 处理重复数据

    统计重复数据 以下我们将统计表中 first_name 和 last_name的重复记录数: mysql> SELECT COUNT(*) as repetitions, last_name, ...

  8. 2018-2019-2 网络对抗技术 20165232 Exp 9 Web安全基础

    2018-2019-2 网络对抗技术 20165232 Exp 9 Web安全基础 实验任务 本实践的目标理解常用网络攻击技术的基本原理,做不少于7个题目,共3.5分.包括(SQL,XSS,CSRF) ...

  9. Java_IO流实验

    实验题目链接:Java第09次实验(IO流) 0. 字节流与二进制文件 我的代码 package experiment.io; import java.io.DataInputStream; impo ...

  10. if && grep

    if    条件  then         Command else         Command fi                               别忘了这个结尾 ——————— ...