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. HTML静态网页 Window.document对象

    一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunment.getElementById(&qu ...

  2. iis上json解析失败404

    控制面板->打开或关闭windows功能->Internet信息服务->万维网服务->应用程序开发功能,勾选上“.net扩展性”和“ASP.NET”,保存后,重启IIS服务器. ...

  3. php课程---练习(联系人信息表)

    做一个联系人表,实现增删改功能 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...

  4. String类StringBuffer类与StringBuilder类gc垃圾回收

    String类的特点 直接赋值和new调用构造方法两种, 直接赋值时会将字符串常量入内存池,当其他变量再赋相同值时,不再在堆空间开辟内存 new构造方法会开辟两块堆内存空间,可以使用intern手工入 ...

  5. oracle 11g 通过在线重定义方式修改表结构

    今天因为要对一套数据库的数据抽取进行io优化,希望通过修改表结构将抽取io降下来,因为抽取只针对标签HAVE_FLAG为"0"的值进行抽取,抽取之后更新HAVE_FLAG为其他值, ...

  6. 代码规范[iOS]

    一.文件系统 二.代码结构 三.常量  宏 四.布局方式  数据持久化 五.网络 六.类目.延展 七.内存.线程 八.其他 九.版本控制.上线相关 十.附录1(公共库) 十一.附录2(公共类目) 十二 ...

  7. react-native 问题总结

    给npm换源 1.通过config配置指向国内源 npm config set registry http://registry.cnpmjs.org //配置指向源 npm info express ...

  8. python display color output

    起因 在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想 ...

  9. JavaScript 字符 &quot;转换

    后台把一个Json类型的数据当成字符串返回到前台,但是到前台变成了下面的这个样子 "[{"name":"IE","y":72},{ ...

  10. 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结

    List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...