Network-POJ3694并查集+LCA
来源:互联网 发布:网络流行语翻译2016 编辑:程序博客网 时间:2024/04/26 10:29
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) andM(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 computerA andB. Computers are numbered from 1 toN. 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, thei-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
题意:一个网络管理员管理一个网络,网络中的电脑直接或间接的相连接,管理员有Q次操作,每次向网络中建立一条新边,向管理员报告桥的个数。
思路:先将网络中的桥求出来,在求的过程中进行并查集缩点,在询问的时候,进行最朴素的LCA查找最近公共祖先,在求的过程中判断节点与父节点是不是在同一个集合中,如果不在同一个集合,说明是桥,则这个桥将不存在,将两个集合合并。
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <queue>#include <algorithm>using namespace std;const int INF = 0x3f3f3f3f;const int Max = 110000;typedef struct Node{int v;int next;}Line;Line Li[Max*4];int Head[Max],top;int dfn[Max],low[Max],vis[Max];int pre[Max],fa[Max];int num;int n,m,Q;void AddEdge(int u,int v){Li[top].v = v; Li[top].next = Head[u];Head[u] = top++;}int Find(int x){return pre[x]==-1?x:pre[x]=Find(pre[x]);}void Union(int x,int y){int Fx = Find(x);int Fy = Find(y);if(Fx!=Fy){pre[Fx]=Fy;}}void dfs(int fat,int u,int dep){dfn[u]=low[u]=dep;fa[u]=fat;vis[u] = 1;for(int i=Head[u];i!=-1;i=Li[i].next){if(Li[i].v!=fat&&vis[Li[i].v]==1){low[u] = min(low[u],dfn[Li[i].v]);}if(vis[Li[i].v]==0){dfs(u,Li[i].v,dep+1);low[u] = min(low[u],low[Li[i].v]);if(low[Li[i].v]<=dfn[u])//并查集缩点{Union(Li[i].v,u);}else{num++;}}}vis[u]=2;}void Judge(int u){int x=Find(u);int y=Find(fa[u]);if(x!=y)//同一集合,则集合合并{num--;pre[x]=y;}}void LCA(int u,int v)//找公共祖先{while(dfn[u]>dfn[v]){Judge(u);u=fa[u];}while(dfn[v]>dfn[u]){Judge(v);v=fa[v];}while(u!=v){Judge(u);Judge(v);u = fa[u];v = fa[v];}}int main(){int z=1;while(~scanf("%d %d",&n,&m)&&(n+m)){top = 0;memset(Head,-1,sizeof(Head));int u,v;for(int i=0;i<m;i++){scanf("%d %d",&u,&v);AddEdge(u,v);AddEdge(v,u);}num = 0 ;memset(vis,0,sizeof(vis));memset(pre,-1,sizeof(pre));dfs(0,1,1);scanf("%d",&Q);printf("Case %d:\n",z++);while(Q--){scanf("%d %d",&u,&v);if(Find(u)!=Find(v)){LCA(u,v);}printf("%d\n",num);}printf("\n");}return 0;}
- Network-POJ3694并查集+LCA
- Network-POJ3694并查集+LCA
- poj3694 LCA+并查集+tarjan求割边
- 7_6_B题 Network题解[POJ3694] (LCA + 求桥 + 并查集)
- 【POJ3694】Network {tarjan+并查集}
- [POJ3694]Network(桥+并查集)
- POJ3694(tarjan缩点+并查集+LCA)
- poj3694 Network 双连通分量、并查集
- POJ_3694 Network Tarjin + LCA + 并查集
- POJ 3694 Network lca + 并查集
- POJ3694 Network 割边+LCA
- POJ3694 Network 割边 LCA
- poj3694 Network 【图论-Tarjan-Lca】
- poj 3694 Network (连通图缩点+LCA+并查集)
- POJ-3694-Network(Tarjan+LCA+并查集)
- POJ3694-Network(Tarjan缩点+LCA)
- poj3694--Network(双连通缩点+lca)
- POJ3694 Network(边双连通+LCA)
- 安全测试工具(持续收集中)
- JavaSE入门学习7:Java基础语法之语句(下)
- Spring面试题一
- 堆排序
- 手机影音
- Network-POJ3694并查集+LCA
- VMware克隆(复制)后网络配置、CentOS设置静态IP、找不到ipv4地址
- java包及其使用
- 点云三维重建--基于两视图的射影重构,仿射重构以及度量重构
- 控制文件和控制文件的自动备份,oracle11g 控制文件备份的延迟特性
- OC和Swift中的static
- 在static table view 加入已经用xib做好的cell
- Tautology(POJ_3295)
- 用java -X 命令查看JVM的配置说明: