poj3694 Network 无向图tarjan求桥+LCA
来源:互联网 发布:装修设计软件下载 编辑:程序博客网 时间:2024/04/24 05:09
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 21 22 321 21 34 41 22 12 31 421 23 40 0
Sample Output
Case 1:10Case 2:20
Source
题意:给定n个点m条边构成的无向图,有k次询问添加(u,v)边问桥的个数。
思路:先tarjan求出桥的个数以及哪些边为桥,然后对于每次询问(u,v),u-> lca(u,v) v->lca(u,v)的路径中如果之前是桥,那么添加(u,v)之后就不是桥了,程序中bri[i]=0,bri_cnt--,详见代码:
(注意:MAXN设大点,不然RE)
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=100000+100;struct Edge{ int v; int next;}edge[MAXN<<2];int n,m,edge_cnt,dfs_clock,top,bri_cnt,k;int head[MAXN],dfn[MAXN],low[MAXN],bri[MAXN<<2],fa[MAXN],ins[MAXN],s[MAXN];void init(){ edge_cnt=dfs_clock=top=bri_cnt=0; memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(ins,0,sizeof(ins)); memset(bri,0,sizeof(bri)); for(int i=1;i<=n;i++) fa[i]=i;}void addedge(int u,int v){ edge[edge_cnt].v=v; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++;}void tarjan(int u,int f){ dfn[u]=low[u]=++dfs_clock; ins[u]=1; s[top++]=u; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==f) continue; if(!dfn[v]) { fa[v]=u; tarjan(v,u); low[u]=min(low[u],low[v]); if(low[v]>dfn[u]) bri[v]=1,bri_cnt++; } else if(ins[v]) low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { ins[u]=0; while(s[--top]!=u) ins[s[top]]=0; }}void lca(int u,int v){ while(dfn[u]>dfn[v]) { if(bri[u]) bri[u]=0,bri_cnt--; u=fa[u]; } while(dfn[v]>dfn[u]) { if(bri[v]) bri[v]=0,bri_cnt--; v=fa[v]; } while(u!=v) { if(bri[u]) bri[u]=0,bri_cnt--; if(bri[v]) bri[v]=0,bri_cnt--; u=fa[u],v=fa[v]; }}int main(){ //freopen("text.txt","r",stdin); int kase=0; while(~scanf("%d%d",&n,&m) && n+m) { kase++; printf("Case %d:\n",kase); int u,v; init(); for(int i=0;i<m;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } tarjan(1,1); scanf("%d",&k); for(int i=0;i<k;i++) { scanf("%d%d",&u,&v); lca(u,v); printf("%d\n",bri_cnt); } printf("\n"); } return 0;}
- poj3694 Network 无向图tarjan求桥+LCA
- POJ3694 Network 无向图的桥
- poj3694 Network 【图论-Tarjan-Lca】
- POJ3694-Network(Tarjan缩点+LCA)
- poj3694+hdu2460 求桥+缩点+LCA/tarjan
- HDU 2460 Network 求桥(tarjan)+LCA
- POJ 1144 Network Tarjan 求无向图的割点的个数 Tarjan 模板题
- poj3694 Network Tarjan+树链剖分
- hdu2460 poj3694 求桥 + 求lca
- UVA 315 Network(Tarjan求无向图割点)
- 7_6_B题 Network题解[POJ3694] (LCA + 求桥 + 并查集)
- POJ 1144 - Network 用tarjan求无向图的割点
- hdu4738 Caocao's Bridges 无向图 tarjan 求桥
- hdu4738Caocao's Bridges tarjan求无向图的桥
- hdoj 4738 tarjan求无向图的桥
- Tarjan求无向图桥和割点
- POJ 题目3694 Network(tarjan,LCA,求桥)
- poj3694 Network Tarjan(树链剖分)
- HDOJ1166 敌兵布阵 【线段树】
- CentOS 6.5 Linux中为firefox安装FlashPlayer步骤
- SNMP Private Enterprise Number 申请流程
- ExtJS教程(5)---Ext.data.Model之高级应用
- 开闭原则
- poj3694 Network 无向图tarjan求桥+LCA
- Qt5官方demo解析集25——Extending QML - Methods Example
- BIEE在creating domain步骤停止的解决办法
- 较详细的介绍JNI
- Hadoop笔记(一)
- 使用vb脚本将指定的网址设置为兼容性视图_IE10
- input标签的hidden属性的应用及作用
- spring MVC原理
- watchdog的正确用法