天天看點

POJ3694--Network

Description

A network administrator manages a large network. The network consists of N computers and M links between pairs of computers. Any pair of computers are connected directly or indirectly by successive links, so data can be transformed between any two computers. The administrator finds that some links are vital to the network, because failure of any one of them can cause that data can't be transformed between some computers. He call such a link a bridge. He is planning to add some new links one by one to eliminate all bridges.

You are to help the administrator by reporting the number of bridges in the network after each new link is added.

Input

The input consists of multiple test cases. Each test case starts with a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤ 200,000).

Each of the following M lines contains two integers A and B ( 1≤ A ≠ B ≤ N), which indicates a link between computer A and B. Computers are numbered from 1 to N. It is guaranteed that any two computers are connected in the initial network.

The next line contains a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.

The i-th line of the following Q lines contains two integer A and B (1 ≤ A ≠ B ≤ N), which is the i-th added new link connecting computer A and B.

The last test case is followed by a line containing two zeros.

Output

For each test case, print a line containing the test case number( beginning with 1) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i new links are added. Print a blank line after the output for each test case.

Sample Input

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

Sample Output

Case 1:
1
0

Case 2:
2
0
       
/*
    不錯一道題
    題意:給出一幅圖,裡面有橋,給出Q個操作,    每個操作會添加一條邊,問每次操作後圖中橋的個數
    每次求橋會太慢
    考慮一下dfs樹,樹邊肯定是橋,然後每連上x,y,就會形成一個環,這個環内的邊就全部都不是割邊
    是以隻要找到x,y的lca,把這個路徑上的橋标記為否即可
 圖連通
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 400080
int first[maxn],nxt[maxn],vv[maxn],f[maxn],vis[maxn],dfn[maxn],low[maxn];
bool isbridge[maxn];
int e,cnt,bridgenum;
void addEdge(int u,int v)
{
 vv[e] = v; nxt[e] = first[u]; first[u] = e++;
 vv[e] = u; nxt[e] = first[v];  first[v] = e++;
}
int find(int x)
{
 if(x == f[x]) return x;
 return f[x] = find(f[x]);
}
void Union(int x,int y)
{
 int fx = find(x);
 f[y] = fx;
}
void Tarjan(int u,int fa)
{
 f[u] = u;
 vis[u] = 1;
 dfn[u] = low[u] = ++cnt;
 for(int i = first[u];i != -1;i = nxt[i])
 {
  int v = vv[i];
  if(vis[v] == 1 && v != fa)
  {
   low[u] = min(low[u],dfn[v]);
  }
  if(vis[v] == 0)
  {
   Tarjan(v,u);
   Union(u,v);
   low[u] = min(low[u],low[v]);
   if(low[v] > dfn[u])  
   {
    isbridge[v] = 1;
    bridgenum++;
   }
  }
 }
 vis[u] = 2;
}
void lca(int x,int y)
{
 if(dfn[x] < dfn[y]) swap(x,y);
 while(dfn[x] > dfn[y])
 {
  if(isbridge[x])
  {
   bridgenum--;
   isbridge[x] = 0;
  }
  x = f[x];
 }
 while(x != y)
 {
  if(isbridge[y]) {bridgenum--; isbridge[y] = 0;}
  y = f[y];
 }
}
int main()
{
 //freopen("in.txt","r",stdin);
 int n,m;
 int cas = 0;
 while(scanf("%d%d",&n,&m)==2 &&(n||m))
 {
  cas++;
  memset(vis,0,sizeof(vis));
  memset(dfn,0,sizeof(dfn));
  memset(first,-1,sizeof(first));
  memset(isbridge,0,sizeof(isbridge));
  e = cnt = bridgenum = 0;
  for(int i = 1;i <= m;i++)
  {
   int u,v;
   scanf("%d%d",&u,&v);
   addEdge(u,v);
  }
  Tarjan(1,-1);
  printf("Case %d:\n",cas);
  int q; scanf("%d",&q);
  for(int i = 1;i <= q;i++)
  {
   int u,v;
   scanf("%d%d",&u,&v);
   lca(u,v);
   printf("%d\n",bridgenum);
  }
  printf("\n");
 }
 return 0;
}