poj 3694 Network
来源:互联网 发布:金山数据恢复手机版 编辑:程序博客网 时间:2024/04/30 15:46
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
【分析】
tarjan求桥+lca
利用dfn的神奇性质
握了个大草,这道题好像忽略重边的样子
我判了重边一直WA,不判就A了…迷丫
【代码】
//poj 3694 Network#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=100005;int n,m,q,cnt,tim,ans;int head[mxn],dfn[mxn],low[mxn],father[mxn],baba[mxn];struct edge {int to,next;} f[mxn*5];inline void add(int u,int v){ f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;}inline int find(int x){ if(x!=father[x]) father[x]=find(father[x]); return father[x];}inline void tarjan(int u,int fa){ int i,j,v,flag=0; dfn[u]=low[u]=++tim; for(i=head[u];i;i=f[i].next) { v=f[i].to; if(!dfn[v]) { baba[v]=u; tarjan(v,u); low[u]=min(low[u],low[v]); if(dfn[u]<low[v]) ans++; else father[find(v)]=find(u); } else if(v!=fa) low[u]=min(low[u],dfn[v]); }}inline int lca(int x,int y){ if(dfn[x]<dfn[y]) swap(x,y); //x更深 while(dfn[x]>dfn[y]) { if(find(x)!=find(baba[x])) ans--,father[find(x)]=find(baba[x]); x=baba[x]; } while(y!=x) { if(find(y)!=find(baba[y])) ans--,father[find(y)]=find(baba[y]); y=baba[y]; } return ans;}int main(){ int i,j,u,v,T=0; while(scanf("%d%d",&n,&m) && n && m) { printf("Case %d:\n",++T); cnt=tim=ans=0; M(head);M(baba);M(dfn);M(low); fo(i,1,n) father[i]=i; while(m--) { scanf("%d%d",&u,&v); add(u,v),add(v,u); } tarjan(1,0); scanf("%d",&q); while(q--) { scanf("%d%d",&u,&v); printf("%d\n",lca(u,v)); } 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
- Part 81 - Range attribute in asp.net mvc
- c#学习笔记之十 socket编程的listen()方法参数的理解,即backlog
- Spark MLlib特征处理:StringToIndex 字符串索引---原理及实战
- Python urllib和urllib2的入门
- C/C++ Lua Parsing Engine
- poj 3694 Network
- 鸡尾酒疗法
- 哈欠:微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?
- 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如 2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制
- 基于SuperSocket实现的WebSocket服务器 和Unity中使用Websocket
- idea2016中tomcat配置及运行
- Part 82 - Creating custom validation attribute in asp.net mvc
- SQLserver2008数据表的修改操作
- 数据结构1