题意及思路:https://blog.csdn.net/mitsuha_/article/details/86482347

如果一条边(u, v),v的拓扑序小于u, 那么(u, v)会形成环,要反向。

代码:

#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int maxn = 100010;
vector<int> G[maxn];
vector<int> ans;
int limit;
int deg[maxn], s[maxn];
queue<int> q;
struct edge {
int x, y, val, id;
};
edge a[maxn];
void add(int x, int y) {
G[x].push_back(y);
deg[y]++;
}
int n, m;
bool topsort() {
ans.clear();
int tot = 0;
memset(deg, 0, sizeof(deg));
for (int i = 1; i <= n; i++)
G[i].clear();
for (int i = 1; i <= m; i++) {
if(a[i].val > limit) {
add(a[i].x, a[i].y);
}
}
for (int i = 1; i <= n; i++)
if(deg[i] == 0) {
q.push(i);
s[i] = ++tot;
}
while(q.size()) {
int x = q.front();
q.pop();
for (auto y : G[x]) {
deg[y]--;
if(deg[y] == 0) {
q.push(y);
s[y] = ++tot;
}
}
}
for (int i = 1; i <= n; i++) if(deg[i] > 0) return 0;
for (int i = 1; i <= m; i++) {
if(a[i].val <= limit) {
if(s[a[i].x] > s[a[i].y])
ans.push_back(a[i].id);
}
}
return 1;
}
int main() {
int x, y, z;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
a[i] = (edge){x, y, z, i};
}
int l = 0, r = 1e9;
while(l < r) {
limit = (l + r) >> 1;
bool flag = topsort();
if(!flag) l = limit + 1;
else r = limit;
}
limit = l;
topsort();
printf("%d %d\n", l, ans.size());
for (int i = 0; i < ans.size(); i++)
printf("%d ", ans[i]);
printf("\n");
}

  

Codeforces 1100E 拓扑排序的更多相关文章

  1. CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)

    ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...

  2. CodeForces - 721C 拓扑排序+dp

    题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c     //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...

  3. National Property CodeForces - 875C (拓扑排序)

    大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...

  4. Codeforces 1159E 拓扑排序

    题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...

  5. E - E CodeForces - 1100E(拓扑排序 + 二分)

    E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方 ...

  6. CodeForces - 1100E 二分+拓扑排序

    题意: 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Inpu ...

  7. Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序

    B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...

  8. Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序

    C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...

  9. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

随机推荐

  1. 【LeetCode】随机化算法 random(共6题)

    [384]Shuffle an Array(2019年3月12日) Shuffle a set of numbers without duplicates. 实现一个类,里面有两个 api,struc ...

  2. 零基础python教程-Python解释器是什么?

    当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...

  3. c++使用boost库遍历文件夹

    1.只在当前目录下遍历 #include <boost/filesystem.hpp> string targetPath="/home/test/target"; b ...

  4. Nmon监控服务端性能

    一.安装1.查看服务器操作系统的版本信息 lsb_release -a cat /etc/*release2.下载 a.nmon下载地址:http://nmon.sourceforge.net/pmw ...

  5. springmvc把对象放到session中

    1:首先把创建的对象放到Map中, @RequestMapping("/testSession")    public String testSession(Map<Stri ...

  6. spring-boot整合Mybatis案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 3.2.5 2.Maven Plugin管理 <?xml version="1.0&q ...

  7. php strtok()函数 语法

    php strtok()函数 语法 作用:逐一分割字符串大理石构件 语法:strtok(string,split) 参数: 参数 描述 string 必需.规定要分割的字符串. split 必需.规定 ...

  8. 第二章--k-近邻算法(kNN)

    一.k-近邻算法(kNN) 采用测量不同特征值之间的距离方法进行分类 工作原理: 存在一个样本数据集合(训练样本集),并且样本集中每个数据都存在标签,即我们知道样本集中每一数据与所属分类的对应关系.输 ...

  9. 关于exe文件传递参数方法

    段代码手工折叠 {$REGION 'Designer Managed Code'} ............ {$ENDREGION} 昨天同事问到,delphi里exe文件如何传递参数? 因为手头装 ...

  10. 从Java future 到 Guava ListenableFuture实现异步调用

    从Java future 到 Guava ListenableFuture实现异步调用 置顶 2016年04月24日 09:11:14 皮斯特劳沃 阅读数:17570 标签: java异步调用线程非阻 ...