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 ...
随机推荐
- ubuntu安装配置jdk tomcat mysql ...
安装之前大家一定要检查好各个版本问题 以免造成不兼容. (一)下载所需安装包: jdk-7u76-linux-x64.tar.gz apache-tomcat-7.0.63.tar.gz MySQL- ...
- NGUI事件监听之UIEventListener的使用
NGUI的事件绑定可以使用 UIButtonMessage 在一个游戏对象上添加Button Message组件: 在Button Message组件上添加要通知的游戏对象上所挂载的脚本的方法 Tar ...
- MySQL Server-id踩到的坑
最近踩到一个说大不大,说小不小的坑,在此分享出来给各位同学.事情是这样的,线上有2台服务器,1主1从.A -> B,B服务器从A服务器同步数据.每天使用xtrabackup在B服务器上面进行全备 ...
- flume+elasticsearch
项目的日志服是使用flume+elasticsearch 但是运维大哥告诉我,经常会大量往外发包,以至流量超标.问我是不是程序有问题.当时我拍着胸膛说:肯定不是.他说,可能是 服务器被攻击了,让我换一 ...
- Excel报表开发
读取Excel数据 /// <summary> /// 封装方法 /// </summary> /// <param name="path">& ...
- html background 背景颜色美化 类似毛玻璃
制作高大上背景颜色 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 【转】Gvim开发环境配置笔记--Windows篇
配置文件(vimrc) set nocompatible set nu! set cursorline colorscheme murphy " vim 自身命令行模式智能补全 set wi ...
- IOS网络第一天 - 02SDWebImage
**************SDWebImage加载图片 #import "HMAppsViewController.h" #import "HMApp.h" ...
- MySQL查询优化:查询慢原因和解决技巧
在开发的朋友特别是和mysql有接触的朋友会碰到有时mysql查询很慢,当然我指的是大数据量百万千万级了,不是几十条了,下面我们来看看解决查询慢的办法. MySQL查询优化:查询慢原因和解决方法 会经 ...
- mybatis父子表批量插入
<!--父子表批量插入 --> <insert id="insertBatch" parameterType="com.niwopay.dto.beni ...