Network-POJ3694并查集+LCA
| Time Limit: 5000MS | Memory Limit: 65536K | |
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) 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 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 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
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的更多相关文章
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu6074[并查集+LCA+思维] 2017多校4
看了标答感觉思路清晰了许多,用并查集来维护全联通块的点数和边权和. 用另一个up[]数组(也是并查集)来保证每条边不会被重复附权值,这样我们只要将询问按权值从小到大排序,一定能的到最小的边权和与联通块 ...
- POJ3694:Network(并查集+缩点+lca)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13172 Accepted: 4774 题目链接:htt ...
- POJ3694 Network - Tarjan + 并查集
Description 给定$N$个点和 $M$条边的无向联通图, 有$Q$ 次操作, 连接两个点的边, 问每次操作后的图中有几个桥 Solution 首先Tarjan找出边双联通分量, 每个双联通分 ...
- Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)
题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...
- POJ 3694 Network(并查集缩点 + 朴素的LCA + 无向图求桥)题解
题意:给你一个无向图,有q次操作,每次连接两个点,问你每次操作后有几个桥 思路:我们先用tarjan求出所有的桥,同时我们可以用并查集缩点,fa表示缩点后的编号,还要记录每个节点父节点pre.我们知道 ...
- HDU6074 Phone Call (并查集 LCA)
Phone Call Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Tota ...
- POJ 2236 Wireless Network(并查集)
传送门 Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 24513 Accepted ...
- poj 2236:Wireless Network(并查集,提高题)
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16065 Accepted: 677 ...
随机推荐
- Spinner的深入学习
简介: spinner是一个列表选择框,会在用户选择后,展示一个列表供用户进行选择.Spinner是ViewGroup的间接子类,它和其他的Android控件一样,数据需要使用Adapter进行封装. ...
- IOS第二天多线程-05NSOperationQueue 暂停,和恢复队列任务
*********** #import "HMViewController.h" @interface HMViewController () <UITableViewDel ...
- Windows NT驱动程序的基本结构和实例
Windows驱动程序分为两类:一类是不支持即插即用功能的NT式驱动程序:另一类是支持即插即用功能的WDM驱动程序. NT式驱动的基本结构: 1)驱动加载过程与驱动入口函数DriverEntry: 驱 ...
- 【iCore3 双核心板_ uC/OS-III】例程八:互斥信号量
实验指导书及代码包下载: http://pan.baidu.com/s/1geDzqqn iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- whether the computers in a cluster share access to the same disks
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION In the literature, cl ...
- 经典C#编程理解,概要,经典
一.NET框架 ADO.NET微软提供的一组类库,可以帮助程序员和数据库交互. CLS(公共语言规范) CTS(通用语言类型) 类库: 可以看成一个承载了N个类的容器. 类库和命名空间: 一个类库对应 ...
- UINavigation Bar中使用UIcollectionView,在UIcollectionView的顶端和低端出现空白的问题
在含有UINavigation Bar的视图中,使用UIcollectionView,会在UIcollectionView的顶端和低端出现空白,需要添加下面 一句代码,空白即可消失 //如果不加这 ...
- 随手记一次利用开源zxing生成带嵌入logo的二维码图片
之前就在项目里面用过zxing生成二维码,最近另一个项目同样需要用到二维码,故重新在学了学利用zxing生成二维码 接下来先做准备工作了,因为我是用vs2013上开发的,故选择了.net4.5版本的z ...
- Python之路-python(面向对象进阶(模块的动态导入、断言、Socket Server))
模块的动态导入 断言 Socket Server 一.模块的动态导入 class C(object): def __init__(self): self.name = "zhangsan&q ...
- 翻译:Lisp Style Tips for the Beginner - Heinrich Taube
原文:Lisp Style Tips for the Beginner 本篇文章是一篇非正式的摘要,旨在帮助新手写出高效.易读的Lisp代码. 1 赋值 1.1 避免使用eval.赋值是Lisp内 ...