Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5874 | Accepted: 2052 |
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
题意:给出一个无向连通图,给出若干个询问,对于每个询问(a,b)表示加了(a,b)这条边后,图中的桥还有多少条。
思路:先用tarjan算法求出原图有多少条桥,对于每加入一条边,图中必定形成了环,若有的边是原来的桥,现在在环中,则这条边不再是桥。tarjan时标记那条边是桥,然后在环中找桥,一直找到lca(a,b)。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <map> #include <cstdlib> #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define ll long long using namespace std; const int maxn=100005; const int maxm=200005; struct node { int v,next; }edge[maxm*2]; int G[maxn],pre[maxn],low[maxn],dfn[maxn]; bool bg[maxn],vis[maxn]; int n,m,num,sum,cnt,q; void init() { memset(G,-1,sizeof(G)); num=0; } void add(int u,int v) { edge[num].v=v; edge[num].next=G[u]; G[u]=num++; } void input() { int a,b; while(m--) { scanf("%d%d",&a,&b); add(a,b); add(b,a); } } void dfs(int u,int fa) //tarjan算法找桥 { dfn[u]=low[u]=++cnt; bool flag=true; for(int i=G[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa&&flag) {flag=false; continue;} if(!dfn[v]) { pre[v]=u; dfs(v,u); low[u]=min(low[u],low[v]); if(low[v]>dfn[u]) { sum++; bg[v]=true; } } else if(dfn[v]<low[u]) low[u]=dfn[v]; } } void tarjan() { memset(dfn,0,sizeof(dfn)); memset(bg,false,sizeof(bg)); cnt=sum=0; pre[1]=-1; dfs(1,-1); } int lca(int a,int b) //求两点的lca { memset(vis,false,sizeof(vis)); while(b!=-1) { vis[b]=true; b=pre[b]; } while(!vis[a]) a=pre[a]; return a; } void solve() { int a,b,c; scanf("%d",&q); while(q--) { scanf("%d%d",&a,&b); c=lca(a,b); while(a!=c) { if(bg[a]) { bg[a]=false; sum--; } a=pre[a]; } while(b!=c) { if(bg[b]) { bg[b]=false; sum--; } b=pre[b]; } printf("%d\n",sum); } } int main() { int c=0; while(scanf("%d%d",&n,&m),n||m) { init(); input(); tarjan(); printf("Case %d:\n",++c); solve(); } return 0; }