Codeforces 1100E 拓扑排序
题意及思路: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 拓扑排序的更多相关文章
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- National Property CodeForces - 875C (拓扑排序)
大意: n个字符串, 每次操作选出一种字符全修改为大写, 求判断能否使n个字符串字典序非降. 建源点s, 汇点t, s与所有必须转大写的连边, 必须不转大写的与t连边. #include <io ...
- Codeforces 1159E 拓扑排序
题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...
- E - E CodeForces - 1100E(拓扑排序 + 二分)
E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方 ...
- CodeForces - 1100E 二分+拓扑排序
题意: 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Inpu ...
- 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 ...
- 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 ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
随机推荐
- Maven仓库配置淘宝Maven镜像仓库
settings.xml文件里面配置 <mirrors> <mirror> <id>alimaven</id> <name ...
- ubuntu安装Scala
参考地址:https://blog.csdn.net/qq_36743482/article/details/78317549 1.下载Scala:http://www.scala-lang.org/ ...
- Elastic Search快速入门
https://blog.csdn.net/weixin_42633131/article/details/82902812 通过这个篇文章可以快速入门,快速搭建一个elastic search de ...
- BZOJ3207 花神的嘲讽计划I
Time Limit: 10 Sec Memory Limit: 128 MB Summary 给你一个模式串P,q个询问,对每个询问回答从Pl到Pr是否存在与给定串相同的子串,同时有所有的给定串长度 ...
- UVa1636 Headshot 【迁移自洛谷博客】
说明:小蒟蒻hkk现在正在做一些概率的题目,由于这方面和数学还有点关系,所以需要一些数学的思维,也需要表述出来,如夏军所述"把自己给讲懂",所以写了些blog,主要为帮助自己理解. ...
- 更好的在 Git 项目中保存大文件(Git LFS 的使用)
珠玉在前, 大家可以参考 Git LFS的使用 - 简书 为什么要用 Git LFS 原有的 Git 是文本层面的版本控制, 为代码这种小文件设计的, 保存大文件会导致 repo 非常臃肿, push ...
- 【leetcode】553. Optimal Division
题目如下: 解题思路:这是数学上的一个定理.对于x1/x2/x3/..../xN的序列,加括号可以得到的最大值是x1/(x2/x3/..../xN). 代码如下: class Solution(obj ...
- Java调用明华RF读写器DLL文件的方法
首先jdk必须得是32位的,IDE也必须是32位的(我用的idea,所以为了使用32位的,下载了2018年1月版本的). 明华RF读写器演示文件提供了一份名为mwrf32.dll的动态链接库文件 ja ...
- sublime text 3 快捷操作
快捷键:1.通用 ↑↓← → 上下左右移动光标 Alt 调出菜单 Ctrl + Shift + P 调出命令板(Command Palette) Ctrl + ` 调出控制台 2.编辑 Ctrl + ...
- Centos7安装Nginx1.14.0
一.官网下载 http://nginx.org/en/download.html 版本说明: Nginx官网提供了三个类型的版本 Mainline version:Mainline 是 Nginx 目 ...