LCA+桥poj3694
来源:互联网 发布:比较好的淘宝女装店 编辑:程序博客网 时间:2024/05/01 02:30
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
先找出图中的桥,然后并查集缩点,询问的时候,找出连点的LCA,然后把经过的连通分量再合并
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;int n,m,q;int pre[maxn],low[maxn],head[maxn],dfs_clock,fa[maxn],cnt;vector<int> g[maxn];int find(int x){ if(x==head[x])return x; return head[x]=find(head[x]);}void unite(int a,int b){ int x=find(a),y=find(b); head[y]=x;}void dfs(int u,int f){ low[u]=pre[u]=++dfs_clock; int len=g[u].size(); for(int i=0;i<len;i++) { int v=g[u][i]; if(!pre[v]) { fa[v]=u; dfs(v,u); low[u]=min(low[u],low[v]); if(low[v]>pre[u])cnt++; else unite(u,v); } else if(pre[v]<pre[u]&&v!=f) low[u]=min(low[u],pre[v]); }}void find_bcc(){ memset(pre,0,sizeof(pre)); for(int i=0;i<=n;i++)head[i]=i; dfs_clock=cnt=0; dfs(1,-1);}void LCA(int a,int b){ while(a!=b) { while(pre[a]>=pre[b]&&a!=b) { if(find(a)!=find(fa[a])) { cnt--; unite(fa[a],a); } a=fa[a]; } while(pre[b]>=pre[a]&&a!=b) { if(find(b)!=find(fa[b])) { cnt--; unite(fa[b],b); } b=fa[b]; } } printf("%d\n",cnt);}int main(){ int cas=1; while(scanf("%d%d",&n,&m)!=EOF,n||m) { for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } find_bcc(); scanf("%d",&q); printf("Case %d:\n",cas++); while(q--) { int l,r; scanf("%d%d",&l,&r); if(find(l)==find(r))printf("%d\n",cnt); else LCA(l,r); } printf("\n"); } return 0;}
- LCA+桥poj3694
- hdu2460 poj3694 求桥 + 求lca
- poj3694 连通图+LCA
- poj3694+hdu2460 求桥+缩点+LCA/tarjan
- poj3694 Network 无向图tarjan求桥+LCA
- POJ3694-Network(LCA+边联通分量+桥)
- POJ3694 Network 割边+LCA
- poj3694(割边,LCA)
- POJ3694 Network 割边 LCA
- poj3694 Network 【图论-Tarjan-Lca】
- 7_6_B题 Network题解[POJ3694] (LCA + 求桥 + 并查集)
- POJ3694-Network(Tarjan缩点+LCA)
- hdu2460&&poj3694 缩点+lca变形
- poj3694--Network(双连通缩点+lca)
- Network-POJ3694并查集+LCA
- POJ3694 Network(边双连通+LCA)
- poj3694 LCA+并查集+tarjan求割边
- Network-POJ3694并查集+LCA
- HDU2489 Minimal Ratio Tree 【DFS】+【最小生成树Prim】
- AndroidStudio单元测试——instrumentation
- 双方发生法萨芬
- poj 1598 Excuses, Excuses!
- HttpClient使用详解
- LCA+桥poj3694
- 12.jQuery UI Cookie插件
- Latex 中的若干问题
- OpenSessionInViewFilter 的配置及替代方案
- 高德地图搭建与实现
- hdu 1708 Fibonacci String
- 线性表----队列(顺序表示)的基本操作
- org.springframework.orm.ibatis.support.SqlMapClientDaoSupport在哪个jar包下
- PHP中spl_autoload_register()函数的用法