poj3694

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7300 Accepted: 2651

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
 
题意:给你一个连通图,然后再给你n个询问,每个询问给一个点u,v表示加上u,v之后又多少个桥。一个最容易想到的办法就是先加边找桥,加边找桥,这样可定超时。那么就可以缩点,因为如果一条边不是桥那么无论怎么加边他肯定都不会变成桥,这样我吧不是桥的点缩成一个点。这样全图就都是桥,这样的话,我们就在加的遍里面去找如果加的边是同一个点,那么,肯定不会减少桥,但是如果不是同一个,那么桥肯定减少~。
虽然知道思路,但至今还是WA。
 
WA代码:
var
  a:array [1..5000,1..500] of longint;
  i,j,m,n,x,y,z,d,q,ans,t,p,leaf:longint;
  low,dfn,c,st,bcc:array [1..5000] of longint;
  b:array [1..10000,1..2] of longint;
  ff,k:array [1..5000,1..5000] of boolean;
  f:array [1..5000] of boolean;


function min(x,y:longint):longint;
begin
  if x<y then
  exit(x);
  exit(y);
end;


procedure cut(x,y:longint);
begin
  if not k[x,y] then
  begin
    ff[y,x]:=false;
    ff[x,y]:=false;
    inc(t);
  end;
end;


procedure find(x,y:longint);
var
  i,v,xx:longint;
begin
  inc(d);
  low[y]:=d;
  dfn[y]:=d;
  f[y]:=true;
  for i:=1 to c[y] do
  if x<>a[y,i] then
  begin
    v:=a[y,i];
    if not f[v] then
    begin
      find(y,v);
      if low[v]=dfn[v] then
      cut(y,v);
      low[y]:=min(low[y],low[v]);
    end
    else
    low[y]:=min(low[y],dfn[v]);
  end;
end;


procedure dfs(x:longint);
var
  i:longint;
begin
  f[x]:=true;
  bcc[x]:=p;
  for i:=1 to c[x] do
  if ff[x,a[x,i]] and not f[a[x,i]] then
  dfs(a[x,i]);
end;


begin
  readln(m,n);
  while (m<>0) and (n<>0) do
  begin
    fillchar(c,sizeof(c),0);
    fillchar(a,sizeof(a),0);
    fillchar(k,sizeof(k),false);
    fillchar(ff,sizeof(ff),false);
    for i:=1 to n do
    begin
      readln(x,y);
      inc(c[x]);
      inc(c[y]);
      a[x,c[x]]:=y;
      a[y,c[y]]:=x;
      if ff[x,y] then
      begin
        k[x,y]:=true;
        k[y,x]:=true;
      end;
      ff[x,y]:=true;
      ff[y,x]:=true;
    end;
    readln(z);
    for j:=1 to z do
    begin
      readln(x,y);
      inc(c[x]);
      inc(c[y]);
      a[x,c[x]]:=y;
      a[y,c[y]]:=x;
      if ff[x,y] then
      begin
        k[x,y]:=true;
        k[y,x]:=true;
      end;
      ff[x,y]:=true;
      ff[y,x]:=true;
      d:=0;
      t:=0;
      fillchar(f,sizeof(f),false);
      for i:=1 to m do
      if not f[i] then
      find(-1,i);
      writeln(t);
    end;
    readln(m,n);
  end;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值