### POJ 3694 Network(無向圖求橋+重邊處理+LCA)

bridge[i] 是指  i 和 father[i] 是一座橋

bridge[i] 是指  i 和 father[i] 是一座橋

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
usingnamespace std;
#define INF 0xfffffff
#define min(a,b) (a<b?a:b)
#define maxn 100005
int m, n, Time, ans;
int dfn[maxn], low[maxn], Father[maxn], bridge[maxn];
vector<int> G[maxn];

void init()
{
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(bridge, 0, sizeof(bridge));
memset(Father, 0, sizeof(Father));
Time = ans = 0;

for(int i=0; i<=n; i++)
G[i].clear();
}

void Tarjan(int u,int fa)
{
dfn[u] = low[u] = ++Time;
Father[u] = fa;
int len = G[u].size(), v;

for(int i=0; i<len; i++)
{
v = G[u][i];

if( !low[v] )
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);

if(dfn[u] < low[v])
{
bridge[v] ++;
ans ++;
}
}
elseif(v != fa)
{
low[u] = min(low[u], dfn[v]);

if(dfn[u] < low[v])
{
bridge[v] ++;
ans --;
}
}

}
}
void Lca(int a,int b)
{
if(a == b)
return ;

if(dfn[a] > dfn[b])
{
if( bridge[a] == 1)
{
bridge[a] = 0;
ans --;
}
Lca(Father[a], b);
}
else
{
if(bridge[b] == 1)
{
bridge[b] = 0;
ans --;
}
Lca(a, Father[b]);
}
}

int main()
{
int cas = 1;
while(scanf("%d %d",&n, &m), m+n)
{
int Q, a, b;
init();
while(m --)
{
scanf("%d %d",&a, &b);
G[a].push_back(b);
G[b].push_back(a);
}

scanf("%d", &Q);
Tarjan(1, 0);
//    printf("%d\n", ans);
printf("Case %d:\n", cas ++);
while(Q --)
{
scanf("%d %d",&a, &b);
Lca(a, b);
printf("%d\n", ans);
}
}
return0;
}
/*

4 4
1 2
2 1
2 3
1 4
2
1 2
3 4

*/