HDU-4738 Caocao's Bridges
Caocao's Bridges
tarjan 割边
直接跑权值最小割点
坑:
-
如果图本身不连通,则输出 \(0\)
-
如果图连通,且最小割边为 \(0\),应该输出 \(1\)(至少有一个人去炸)
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e6 + 10;
const int inf = 1e9 + 10;
int head[maxn], to[maxn], nexx[maxn], val[maxn], vp = 1;
int low[maxn], dfn[maxn], tp = 0, ans = inf;
inline void add(int u, int v, int t)
{
vp++;
nexx[vp] = head[u];
head[u] = vp;
val[vp] = t;
to[vp] = v;
}
void tarjan(int now, int pre)
{
low[now] = dfn[now] = ++tp;
for(int i=head[now]; i; i=nexx[i])
{
int nex = to[i];
if(dfn[nex] == 0)
{
tarjan(nex, i);
low[now] = min(low[nex], low[now]);
if(low[nex] > dfn[now])
ans = min(ans, val[i]);
}
else if(i != (pre ^ 1))
low[now] = min(dfn[nex], low[now]);
}
}
void init(int n)
{
for(int i=0; i<=n; i++) dfn[i] = low[i] = head[i] = 0;
ans = inf;
tp = 0;
vp = 1;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
while(cin >> n >> m && (n | m))
{
init(n);
while(m--)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
int cnt = 0;
for(int i=1; i<=n; i++)
{
if(dfn[i] == 0)
{
tarjan(i, -1);
cnt++;
}
}
ans = max(1, ans);
if(cnt > 1) ans = 0;
if(ans == inf) ans = -1;
cout << ans << "\n";
}
return 0;
}