优先队列 + 反向拓扑

//#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. confluence部署

    confluence -- 团队文档的管理平台. 首先要在confluence官网买key. 部署 安装jdk 1.8 环境 查看机器是否自带 java -version,没有再安装. yum ins ...

  2. tomcat 散点杂记

    tomcat有很多细碎的知识点和一些坑点,我将再次记录 域名直指项目 我们经常访问项目都要带上项目目录 eg: http://xwiki.test.com/xwiki or http://jenkin ...

  3. HDU 4725 The Shortest Path in Nya Graph (最短路 )

    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...

  4. java中 在一个异常处理中什么语句块是可多个的

    MM们 异常处理一般格式:捕获异常:try{//代码块}catch(异常类型,例如:Exception e){//需要抛出的异常,例如:e.printStackTrace();}catch(异常类型) ...

  5. sessionStorge和localStorage的使用-踩坑记_09

    sessionStorge的使用 sessionStorage 属性允许你访问一个 session Storage 对象.它与 localStorage 相似,不同之处在于 localStorage ...

  6. java链接Mysql出现警告:Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by

    Java使用mysql-jdbc连接MySQL出现如下警告: Establishing SSL connection without server's identity verification is ...

  7. 问题:解决上传文件IE浏览器弹出下载框bug?

    控制器方法的返回值必须以String返回,再由js处理转换成json对象   $.ajaxFileUpload({ url: "/project/proj_conver_upload&quo ...

  8. leetcode-easy-string-28 Implement strStr()

    mycode   77.15% class Solution(object): def strStr(self, haystack, needle): """ :type ...

  9. IDEA打开最近打开的项目以及关闭项目

    关闭的是当前项目

  10. DatePicker 日期选择器

    用于选择或输入日期 选择日 以「日」为基本单位,基础的日期选择控件 基本单位由type属性指定.快捷选项需配置picker-options对象中的shortcuts,禁用日期通过 disabledDa ...