最近去OJ上做了几道题,以例题为主。其次今晚看了下tarjan的割边的判定的小知识,其中时间戳、搜索树就不说了,在这里引入了一个新的量,“追溯值”low【x】,某些点中时间戳的最小值。最后判定定理感觉自己没有完全理解。明天再看看。
求桥的代码:
#include <cstdio>
#include <algorithm>
#define N 100010
using namespace std;
struct adj
{
int to, next;
} e[2*N];
int head[N], dfn[N], low[N], n, m, cnt = 1, num = 0;
bool bridge[2*N];
inline void ins(int x, int y)
{
e[++cnt].to = y;
e[cnt].next = head[x];
head[x] = cnt;
}
inline void tarjan(int x, int in)
{
dfn[x] = low[x] = ++num;
for(int i = head[x]; i; i = e[i].next)
{
int y = e[i].to;
if(!dfn[y])
{
tarjan(y, i);
low[x] = min(low[x], low[y]); //从子节点继承
if(low[y] > dfn[x]) bridge[i] = bridge[i ^ 1] = 1; //判定
}
else if(i != (in ^ 1)) low[x] = min(low[x], dfn[y]); //非搜索树边却到达了
}
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
ins(x, y);
ins(y, x);
}
for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i, 0);
for(int i = 2; i < cnt; i+= 2) if(bridge[i]) printf("%d %d\n", e[i ^ 1].to, e[i].to);
return 0;
}