2014
01-26

# Network

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.

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.

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.

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

Case 1:
1
0

Case 2:
2
0

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define MAXN 100009

int n,m,cnt,NE,BridgeNum;
int parent[MAXN],low[MAXN],dfn[MAXN];
bool mark[MAXN],isbridge[MAXN];
vector<int>ve[MAXN];

void Tarjan(int u,int father)
{
int flag=0;
low[u]=dfn[u]=++cnt;
mark[u]=true;
int l=ve[u].size();
for(int i=0; i<l; i++)
{
int v=ve[u][i];
if(v==father&&!flag)
{
flag=1;
continue;
}
if(dfn[v]==0)
{
parent[v]=u;
Tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
isbridge[v]=1;
BridgeNum++;
}
}
else if(mark[v])
low[u]=min(low[u],dfn[v]);
}
}

void LCA(int u,int v)
{
while(dfn[u]>dfn[v])
{
if(isbridge[u])
{
BridgeNum--;
isbridge[u]=0;
}
u=parent[u];
}
while(dfn[v]>dfn[u])
{
if(isbridge[v])
{
BridgeNum--;
isbridge[v]=0;
}
v=parent[v];
}
while(u!=v)
{
if(isbridge[u])
{
BridgeNum--;
isbridge[u]=0;
}
if(isbridge[v])
{
BridgeNum--;
isbridge[v]=0;
}
u=parent[u],v=parent[v];
}
}
int main()
{
int u,v,Q,ca=1;
while(scanf("%d%d",&n,&m)&&(n+m))
{
BridgeNum=NE=cnt=0;
for(int i=0; i<=n; i++)
ve[i].clear();
while(m--)
{
scanf("%d%d",&u,&v);
ve[u].push_back(v);
ve[v].push_back(u);
}
memset(isbridge,0,sizeof isbridge);
memset(dfn,0,sizeof dfn);
memset(mark,0,sizeof mark);
for(int i=1; i<=n+1; i++)parent[i]=i;
Tarjan(1,-1);
printf("Case %d:\n",ca++);
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d",&u,&v);
LCA(u,v);
printf("%d\n",BridgeNum);
}
printf("\n");
}
return 0;
}

1. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count

2. L（X [0 .. M-1]，Y [0 .. N-1]）= 1 + L（X [0 .. M-2]，Y [0 .. N-1]）这个地方也也有笔误
应改为L（X [0 .. M-1]，Y [0 .. N-1]）= 1 + L（X [0 .. M-2]，Y [0 .. N-2]）

3. 学算法中的数据结构学到一定程度会乐此不疲的，比如其中的2－3树，类似的红黑树，我甚至可以自己写个逻辑文件系统结构来。