poj3694(割边,LCA)
来源:互联网 发布:恒泰实达程序员怎么样 编辑:程序博客网 时间:2024/04/18 05:27
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
题意:首先给出一个图,然后接下来有q次询问,每次询问都要添加一条边,然后问你有多少条割边。
思路:首先用tarjan预处理出来所有的割边,查询的时候就直接用LCA来实现查询就好。
#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<stack>using namespace std;typedef long long ll;const int N=1000010;struct data{ int to,next;} tu[N*2];int head[N];int ip;int dfn[N], low[N];///dfn[]表示深搜的步数,low[u]表示u或u的子树能够追溯到的最早的栈中节点的次序号int f[N],bridge[N],Dfn[N];int step,n,m,cnt;void init(){ ip=step=cnt=0; memset(head,-1,sizeof(head)); memset(dfn, 0, sizeof(dfn)); memset(Dfn, 0, sizeof(Dfn)); memset(bridge, 0, sizeof(bridge)); for(int i = 1; i <= n; i++) f[i] = i;}void add(int u,int v){ tu[ip].to=v,tu[ip].next=head[u],head[u]=ip++;}void dfs(int u){ dfn[u] = low[u] = ++step; Dfn[u]=Dfn[f[u]]+1; for (int i = head[u]; i !=-1; i=tu[i].next) { int v = tu[i].to; if (!dfn[v]) { f[v]=u; dfs(v); low[u] = min(low[u], low[v]); if(low[v]>dfn[u]) { cnt++; bridge[v]=1; } } else if(v!=f[u]) low[u] = min(low[u], dfn[v]); }}void lca(int u,int v){ while(Dfn[u] > Dfn[v]) { if(bridge[u]) cnt--, bridge[u] = 0; u = f[u]; } while(Dfn[v] > Dfn[u]) { if(bridge[v]) cnt--, bridge[v] = 0; v = f[v]; } while(u != v) { if(bridge[u]) cnt--, bridge[u] = 0; if(bridge[v]) cnt--, bridge[v] = 0; u = f[u]; v = f[v]; }}int main(){ int o=1; while(~scanf("%d%d",&n,&m)&&m+n) { init(); printf("Case %d:\n",o++); while(m--) { int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs(1); int q; scanf("%d",&q); while(q--) { int x,y; scanf("%d%d",&x,&y); lca(x,y); printf("%d\n",cnt); } puts(""); } return 0;}
- poj3694(割边,LCA)
- POJ3694 Network 割边+LCA
- POJ3694 Network 割边 LCA
- POJ3694 Network(边双连通+LCA)
- POJ3694-Network(LCA+边联通分量+桥)
- poj3694--Network(双连通缩点+lca)
- LCA+桥poj3694
- poj3694 连通图+LCA
- POJ 3694 Network(割边+LCA)
- poj3694 Network 边双联通缩点+离线LCA
- poj3694 Network 【图论-Tarjan-Lca】
- 【POJ 3694】 Network(割边<桥>+LCA)
- 7_6_B题 Network题解[POJ3694] (LCA + 求桥 + 并查集)
- POJ3694-Network(Tarjan缩点+LCA)
- hdu2460&&poj3694 缩点+lca变形
- Network-POJ3694并查集+LCA
- hdu2460 poj3694 求桥 + 求lca
- poj3694 LCA+并查集+tarjan求割边
- 排序-冒泡排序和选择排序(golang)
- Java线程池使用说明
- 利用RunTime解决由NSTimer导致的内存泄漏
- Python黑客编程2 入门demo--zip暴力破解
- VS 开发中的字符串转换 (一)
- poj3694(割边,LCA)
- Android SQLiteOpenHelper使用和源码详解
- HDU2175 汉诺塔9
- 深入理解Java虚拟机学习总结(二) 垃圾收集器与内存分配策略
- 小米网技术架构变迁实践
- CSS布局-负边距
- mybatis的mapper配置文件的一般写法。
- Maven仓库
- maven项目中测试代码