标签:跳过 else next stdout ons 枚举 int freopen travel
给你一个有 \(n\) 个点, \(m\) 条边的无向图,每条有边权 \(w_i\) ,现在要选择至多两条边断开,使得 \(S, T\) 不连通,并且使得边权和尽量小。
\(n \le 1000, m \le 30000\)
我们分要选的边数进行考虑。
Dfs
树的路径上,一开始先 Dfs 求出路径,然后依次枚举每条边断开,再用 Dfs
判是否连通就行了,最后把边权取个 \(\min\) 就行了。复杂度是 \(O(n(n+m))\) 。Dfs
树,然后用 Tarjan
求出桥边就行了,然后依次判断这些树边是否为桥边。如果是,那么就是一个合法解,最后把答案和这两条边权和取 \(\min\) 。复杂度也是 \(O(n(n+m))\) 的。这里需要注意实现细节。比如 Dfs
求 \(S \to T\) 路径,我们可以不要求 \(Lca\) ,可以从 \(S\) Dfs
的时候,记下这条路径是否到达了 \(T\) ,因为是树所以路径唯一。
然后会有重边的情况,我们可以第一次枚举到 \(u\) 点的父亲时候跳过,后面都需要更新 lowlink
就行了。
#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
using namespace std;
typedef pair<int, int> PII;
inline bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
inline bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}
inline int read() {
int x = 0, fh = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * fh;
}
void File() {
#ifdef zjp_shadow
freopen ("F.in", "r", stdin);
freopen ("F.out", "w", stdout);
#endif
}
const int N = 1010, M = 30100 << 1, inf = 0x7f7f7f7f;
int Head[N], Next[M], to[M], val[M], e = 1;
inline void add_edge(int u, int v) { to[++ e] = v; Next[e] = Head[u]; Head[u] = e; }
inline void Add(int u, int v) { add_edge(u, v); add_edge(v, u); }
#define Travel(i, u, v) for(int i = Head[u], v = to[i]; i; v = to[i = Next[i]])
int S, T;
vector<int> V, G; bitset<N> vis; bitset<M> ban;
bool Dfs(int u) {
vis[u] = true; if (u == T) return true;
Travel(i, u, v) if (!ban[i] && !vis[v] && Dfs(v))
return V.push_back(i), true;
return false;
}
int clk, dfn[N], lowlink[N]; bitset<M> Bridge;
void Tarjan(int u, int fa = 0) {
dfn[u] = lowlink[u] = ++ clk;
bool fir = true;
Travel(i, u, v) if (!ban[i]) {
if (v == fa && fir) { fir = false; continue ; }
if (!dfn[v]) {
Tarjan(v, u);
chkmin(lowlink[u], lowlink[v]);
if (lowlink[v] > dfn[u]) Bridge[i] = Bridge[i ^ 1] = true;
} else chkmin(lowlink[u], dfn[v]);
}
}
PII Ans;
int main () {
File();
int n = read(), m = read(); S = read(); T = read();
For (i, 1, m) {
int u = read(), v = read(), w = read();
Add(u, v); val[e] = val[e ^ 1] = w;
}
if (!Dfs(S)) return puts("0\n0"), 0; G.swap(V);
int ans = inf;
for (int cur : G) {
ban[cur] = ban[cur ^ 1] = true; vis.reset();
if (!Dfs(S)) {
if (chkmin(ans, val[cur])) Ans = mp(cur >> 1, 0);
ban[cur] = ban[cur ^ 1] = false; continue ;
}
clk = 0; Bridge.reset(); Set(dfn, 0);
For (i, 1, n) if (!dfn[i]) Tarjan(i, 0);
for (int cut : V)
if (Bridge[cut] && chkmin(ans, val[cur] + val[cut])) Ans = mp(cur >> 1, cut >> 1);
V.clear(); ban[cur] = ban[cur ^ 1] = false;
}
if (ans == inf) return puts("-1"), 0;
printf ("%d\n", ans);
if (!Ans.second) printf ("1\n%d\n", Ans.first);
else printf ("2\n%d %d\n", Ans.first, Ans.second);
return 0;
}
Codeforces 700 C. Break Up(Tarjan求桥)
标签:跳过 else next stdout ons 枚举 int freopen travel
原文地址:https://www.cnblogs.com/zjp-shadow/p/9556298.html