传送门
题目描述
给定一个有向图,改变其中某些边的方向,它将成为一个有向无环图。
现在求一个改变边方向的方案,使得所选边边权的最大值最小。
分析
很巧妙的一道题
首先因为是求最大值的最小值,很容易想到二分,所以怎么去构造
c
h
e
c
k
check
check函数呢?
我们去二分
m
i
d
mid
mid,把大于
m
i
d
mid
mid的边加入图中,判断图中是否有环,如果有环,必然不符合条件,因为我无法去更改图中边的方向,如果不存在环,则必然存在解,因为还未加入图中的边我可以随意更改方向
然后我们求出这个图的拓扑序,枚举每一条可能会被修改的边,通过他们拓扑序的大小关系判断这条边是否需要进行修改
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N], e[N], ne[N], w[N], idx;
int n, m;
int in[N], num[N];
bool st[N], sv[N];
VI ans;
struct Edge {
int u, v, w;
} edge[N];
void add(int x, int y, int z) {
ne[idx] = h[x], e[idx] = y, w[idx] = z, h[x] = idx++;
}
bool dfs(int u, int li) {
st[u] = 1, sv[u] = 1;
for (int i = h[u]; ~i; i = ne[i]) {
int y = e[i], z = w[i];
if (z <= li) continue;
if (sv[y] || !dfs(y, li)) return 0;
}
sv[u] = 0;
return true;
}
inline bool check(int now) {
memset(st, 0, sizeof(st));
memset(sv, 0, sizeof(sv));
for (int i = 1; i <= n; i++)
if (!st[i] && !dfs(i, now)) return 0;
return 1;
}
void topsort(int l) {
queue<int> q;
memset(h, -1, sizeof h);
idx = 0;
for (int i = 1; i <= m; i++) {
if (edge[i].w <= l) continue;
add(edge[i].u, edge[i].v, edge[i].w);
in[edge[i].v]++;
}
int tot = 0;
for (int i = 1; i <= n; i++) if (!in[i]) q.push(i);
while (q.size()) {
int t = q.front();
q.pop();
num[t] = ++tot;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
in[j]--;
if (!in[j]) q.push(j);
}
}
for (int i = 1; i <= n; i++) if (!num[i]) num[i] = ++tot;
for (int i = 1; i <= m; i++) {
int x = edge[i].u, y = edge[i].v, z = edge[i].w;
if (z <= l && num[x] > num[y]) ans.pb(i);
}
}
int main() {
memset(h, -1, sizeof h);
read(n), read(m);
for (int i = 1; i <= m; i++) {
read(edge[i].u), read(edge[i].v), read(edge[i].w);
add(edge[i].u, edge[i].v, edge[i].w);
}
int l = 0, r = INF;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
topsort(l);
printf("%d %d\n", l, (int)ans.size());
for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/