题意及思路: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. 前端移动开发--viewpoint

    移动开发时,一般都会在头部加载这样一行meta标签 <meta name="viewport" content="width=device-width, initi ...

  2. Go copy 的使用

    copy 可以将后面的 第2个切片的元素赋值copy 到第一个切片中 package main; import "fmt" func test () { s1 := []int{1 ...

  3. 【串线篇】Mybatis缓存之二级缓存

    1.应用 二级缓存:namespace级别的缓存:SqlSession关闭或者提交以后有效 一级缓存:SqlSession关闭或者提交以后,一级缓存的数据会放在二级缓存中: 二级缓存的使用:mybat ...

  4. Java programming language does not use call by reference for objects!

    Instead, object references are passed by value! A method cannot modify a parameter of a primitive ty ...

  5. boost container

    Boost Container provides additional advantages: (1) The interface of the containers resemble those o ...

  6. python不能运行

    运行python文件出现,报错please select a valid interpreter 这是因为没有选择interpreter  就是更改目录时需要重新选择pytho解析器 解决方法如下 更 ...

  7. Ext js-02 -官方API文档使用

    官方API文档地址: http://docs.sencha.com/extjs/6.5.3/classic/Ext.html 打开网页如下: 1.选择所使用的Ext js版本,后面offline do ...

  8. java File类的使用以及一些函数

    package file; import java.io.File; import java.io.IOException; import org.junit.jupiter.api.Test; /* ...

  9. 【MySQL】实现自增函数sequence

    前言 当前数据库为:mysql由于mysql和oracle不太一样,不支持直接的sequence,所以需要创建一张table来模拟sequence的功能,理由sql语句如下: 步骤 1.创建seque ...

  10. LOJ 2541 「PKUWC2018」猎人杀——思路+概率+容斥+分治

    题目:https://loj.ac/problem/2541 看了题解才会……有三点很巧妙. 1.分母如果变动,就很不好.所以考虑把操作改成 “已经选过的人仍然按 \( w_i \) 的概率被选,但是 ...