POJ 3694 Network
来源:互联网 发布:淘宝大牌原单正品店 编辑:程序博客网 时间:2024/05/21 12:06
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 integersN(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 andB. 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) andQ lines, the i-th of which contains a integer indicating the number of bridges in the network after the firsti new links are added. Print a blank line after the output for each test case.
Sample Input
3 21 22 321 21 34 41 22 12 31 421 23 40 0
Sample Output
Case 1:10Case 2:20
Source
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int N=100005;const int M=200005;int n,m,q,tim,cnt,ans,cas,hd[N],dfn[N],low[N],f[N],pre[N];struct edge{int to,nxt;}v[2*M];void addedge(int x,int y){++cnt;v[cnt].to=y;v[cnt].nxt=hd[x];hd[x]=cnt;}int fnd(int x){if(f[x]!=x)f[x]=fnd(f[x]);return f[x];}void tarjan(int u,int fa){dfn[u]=low[u]=++tim;for(int i=hd[u];i;i=v[i].nxt)if(!dfn[v[i].to]){pre[v[i].to]=u;tarjan(v[i].to,u);low[u]=min(low[u],low[v[i].to]);if(low[v[i].to]>dfn[u])ans++;elsef[fnd(v[i].to)]=fnd(u);}else if(v[i].to!=fa)low[u]=min(low[u],dfn[v[i].to]);}void lca(int x,int y){while(dfn[y]<dfn[x])swap(x,y);while(dfn[y]>dfn[x]){if(fnd(y)!=fnd(pre[y])){ans--;f[fnd(y)]=fnd(pre[y]);}y=pre[y];}while(x!=y){if(fnd(x)!=fnd(pre[x])){ans--;f[fnd(x)]=fnd(pre[x]);}x=pre[x];}}int main(){while(scanf("%d%d",&n,&m)&&n+m>0){printf("Case %d:\n",++cas);ans=0,tim=0,cnt=0;memset(v,0,sizeof(v));memset(pre,0,sizeof(pre));memset(hd,0,sizeof(hd));for(int i=1;i<=n;i++)f[i]=i;while(m--){int x,y;scanf("%d%d",&x,&y);addedge(x,y);addedge(y,x);}for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i,0);scanf("%d",&q);while(q--){int x,y;scanf("%d%d",&x,&y);lca(x,y);printf("%d\n",ans);}printf("\n");}return 0;}
- POJ 3694 Network
- poj 3694 Network
- POJ 3694 Network
- poj 3694 Network
- poj 3694 Network
- POJ 3694 Network
- POJ 3694 Network
- poj 3694 Network
- POJ 3694 Network
- POJ 3694 Network
- Poj 3694 Network
- poj 3694 Network
- POJ 3694 Network
- poj 3694 Network
- POJ 3694 Network
- POJ 3694 Network
- poj 3694 Network
- POJ-3694 NetWork
- 前端工程师必备实用网站
- PHP学习笔记_1110
- 2016 Unicode Conference拾遗(一)
- 反编译odex 适用于5.X
- 【例题】【链表】NKOJ3499【2015多校联训6】密码
- POJ 3694 Network
- iOS10全新方法实现推送+deviceToken无法获取或无效的解决
- springmvc 关于数据绑定
- .net core Web应用启动类
- 中值定理
- openfire 在调试的时候控制台输出为乱码
- iOS10 推送详解
- .net core Web应用启动类
- python_scapy实现ARP扫描