Network
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≤AB ≤ 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 ≤ ABN), 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

2008 Asia Hefei Regional Contest Online by USTC



题意:一个网络管理员管理一个网络,网络中的电脑直接或间接的相连接,管理员有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的更多相关文章

  1. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. hdu6074[并查集+LCA+思维] 2017多校4

    看了标答感觉思路清晰了许多,用并查集来维护全联通块的点数和边权和. 用另一个up[]数组(也是并查集)来保证每条边不会被重复附权值,这样我们只要将询问按权值从小到大排序,一定能的到最小的边权和与联通块 ...

  3. POJ3694:Network(并查集+缩点+lca)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 4774 题目链接:htt ...

  4. POJ3694 Network - Tarjan + 并查集

    Description 给定$N$个点和 $M$条边的无向联通图, 有$Q$ 次操作, 连接两个点的边, 问每次操作后的图中有几个桥 Solution 首先Tarjan找出边双联通分量, 每个双联通分 ...

  5. Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)

    题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...

  6. POJ 3694 Network(并查集缩点 + 朴素的LCA + 无向图求桥)题解

    题意:给你一个无向图,有q次操作,每次连接两个点,问你每次操作后有几个桥 思路:我们先用tarjan求出所有的桥,同时我们可以用并查集缩点,fa表示缩点后的编号,还要记录每个节点父节点pre.我们知道 ...

  7. HDU6074 Phone Call (并查集 LCA)

    Phone Call Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Tota ...

  8. POJ 2236 Wireless Network(并查集)

    传送门  Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 24513   Accepted ...

  9. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

随机推荐

  1. 关于在VS 上发布网站

    在vs 上建立的网站只能用 localhost 进行访问,想要自己本机上的网站发布到IIS上面,这样就可以直接用 IP 地址来访问了,那么照如下的方式做: 1. 首先需要使用vs 发布自己的网站 1. ...

  2. Struts和SpringMVC两种MVC框架比较

    基于Web的MVC framework在J2EE的世界内已是空前繁荣.TTS网站上几乎每隔一两个星期就会有新的MVC框架发布.目前比较好的MVC,老牌的有Struts.Webwork.新兴的MVC框架 ...

  3. flume+elasticsearch

    项目的日志服是使用flume+elasticsearch 但是运维大哥告诉我,经常会大量往外发包,以至流量超标.问我是不是程序有问题.当时我拍着胸膛说:肯定不是.他说,可能是 服务器被攻击了,让我换一 ...

  4. IOS第一天多线程-03线程间通信

    **** #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomi ...

  5. IOS第16天(1,Quartz2D基本图像绘制)

    ***************基本图像绘制 画线 #import "HMLineView.h" @implementation HMLineView - (id)initWithF ...

  6. DHTML概述

    <!-- DHTML概述:动态的HTML.不是一门语言,是多项技术综合体的简称. 其中包含了HTML.CSS.DOM.JavaScript. 这四个技术在动态HTML页面效果定义时,都处于什么样 ...

  7. 小试牛刀2:JavaScript基础题

    JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...

  8. struts2:数据校验,通过Action中的validate()方法实现校验,图解

    根据输入校验的处理场所的不同,可以将输入校验分为客户端校验和服务器端校验两种.服务器端验证目前有两种方式: 第一种 Struts2中提供了一个com.opensymphony.xwork2.Valid ...

  9. WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享

    WinForm容器内控件批量效验是否允许为空?设置是否只读?设置是否可用等方法分享 在WinForm程序中,我们有时需要对某容器内的所有控件做批量操作.如批量判断是否允许为空?批量设置为只读.批量设置 ...

  10. sharding-jdbc-how2work 当当的sharding-jdbc剖析(查询)

    1. 以JDBC作为出发点 1.1 重新实现了JDBC的几个接口 实现javax.sql.DataSource接口  ShardingDataSource实现java.sql.Connection接口 ...