SPOJ-EC_P Critical Edges
Critical Edges
tarjan 割边模板
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define pii pair<int, int>
const int maxn = 710;
int head[maxn], to[maxn * maxn], nexx[maxn * maxn], vp = 1;
int dfn[maxn], low[maxn], tp = 0;
vector<pii>cute;
inline void add(int u, int v)
{
vp++;
nexx[vp] = head[u];
head[u] = vp;
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[now], low[nex]);
if(low[nex] > dfn[now])
cute.push_back({now, nex});
}
else if(i != (pre ^ 1))
low[now] = min(low[now], dfn[nex]);
}
}
void init(int n)
{
for(int i=0; i<=n; i++) dfn[i] = low[i] = head[i] = 0;
cute.clear();
tp = 0;
vp = 1;
}
int main()
{
int t;
scanf("%d", &t);
for(int cas=1; cas<=t; cas++)
{
int n, m;
scanf("%d%d", &n, &m);
init(n);
for(int i=0; i<m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
add(b, a);
}
for(int i=1; i<=n; i++)
if(dfn[i] == 0) tarjan(i, -1);
for(auto &[x, y] : cute)
if(x > y) swap(x, y);
sort(cute.begin(), cute.end());
printf("Caso #%d\n", cas);
if(cute.size()) printf("%d\n", cute.size());
else printf("Sin bloqueos\n");
for(auto [x, y] : cute)
{
if(x > y) swap(x, y);
printf("%d %d\n", x, y);
}
}
return 0;
}