> File Name: 3694.cpp > Author: jiangyuzhu > Mail: 834138558@qq.com > Created Time: 2016/9/14 13:37:27 ************************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; const int maxn = 1e5 + 5, maxm = 2e5 + 5; int head[maxn], dfn[maxn], low[maxn]; struct EDGE{ int to, next; bool vis; }; EDGE edge[maxm * 2]; int tot, dfnum; int btot; int pa[maxn], fa[maxn]; int dep[maxn]; void init(int n) { tot = 0; btot = 0; dfnum = 1; memset(head, -1, sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); for(int i = 1; i <= n; i++) pa[i] = i; } int _find(int x) { if(x == pa[x]) return x; return pa[x] = _find(pa[x]); } void unit(int x, int y) { int rx = _find(x), ry = _find(y); if(rx == ry) return; pa[rx] = ry; } void addedge(int u, int v) { edge[tot].vis = false; edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } void dfs(int u, int depth) { dfn[u] = low[u] = dfnum++; dep[u] = depth; for(int i = head[u]; i != -1; i = edge[i].next){ if(edge[i].vis) continue; edge[i].vis = edge[i ^ 1].vis = true; int v = edge[i].to; if(!dfn[v]){ fa[v] = u; dfs(v, depth + 1); low[u] = min(low[u], low[v]); }else low[u] = min(low[u], dfn[v]); if(low[v] <= dfn[u]) unit(v, u); else btot++; } } int LCA(int u, int v) { int cnt = 0; while(dep[u] > dep[v]){ if(_find(u) != _find(fa[u])) cnt++; unit(u, fa[u]); u = fa[u]; } while(dep[v] > dep[u]){ if(_find(v) != _find(fa[v])) cnt++; unit(v, fa[v]); v = fa[v]; } while(u != v){ if(_find(u) != _find(fa[u])) cnt++; unit(u, fa[u]); if(_find(v) != _find(fa[v])) cnt++; unit(v, fa[v]); u = fa[u]; v = fa[v]; } return cnt; } int main (void) { int n, m; int a, b; int kas = 1; while(~scanf("%d%d", &n, &m) && (n + m)){ init(n); for(int i = 0; i < m; i++){ scanf("%d%d", &a, &b); addedge(a, b); addedge(b, a); } fa[1] = 1; dfs(1, 1); int q;scanf("%d", &q); printf("Case %d:\n", kas++); for(int i = 0; i < q; i++){ scanf("%d%d", &a, &b); if(_find(a) != _find(b)) btot -= LCA(a, b); printf("%d\n", btot); } puts(""); } return 0; }
|