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 integers N(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 and B. Computers are numbered from 1 to N. 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) and Q lines, the i-th of which contains a integer indicating the number of bridges in the network after the first i 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 图论关于桥的模板
给你一个无向图,问每次加入一些边 ,桥的个数
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int maxn = 2e5 + ;
int head[maxn], dfn[maxn], vis[maxn], fa[maxn], low[maxn];
int dcnt, bcnt, tot;
struct node {
int v, next, used;
} edge[ * maxn] ;
int isbridge[maxn];
void add(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
edge[tot].used = ;
head[u] = tot++;
}
void lca(int u, int v) {
if (dfn[u] < dfn[v]) swap(u, v);
while(dfn[u] > dfn[v]) {
if(isbridge[u]) bcnt--;
isbridge[u] = ;
u = fa[u];
}
while(u != v) {
if (isbridge[u]) bcnt--;
if (isbridge[v]) bcnt--;
isbridge[u] = isbridge[v] = ;
u = fa[u], v = fa[v];
}
}
void dfs(int u) {
vis[u] = ;
dfn[u] = low[u] = ++dcnt;
for (int i = head[u]; i != - ; i = edge[i].next )
if (!edge[i].used) {
edge[i].used = edge[i ^ ].used = ;
int v = edge[i].v;
if (!vis[v]) {
fa[v] = u;
dfs(v);
low[u] = min(low[u], low[v]);
if (dfn[u] < low[v]) {
bcnt++;
isbridge[v] = ;
}
} else if (vis[v] == ) low[u] = min(low[u], dfn[v]);
}
vis[u] = ;
}
void init() {
tot = dcnt = bcnt = ;
memset(head, -, sizeof(head));
memset(isbridge, , sizeof());
memset(vis, , sizeof(head));
}
int main() {
int cas = , n, m, q;
while(scanf("%d%d", &n, &m) != EOF) {
if (n == && m == ) break;
init();
for (int i = ; i < m ; i++) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
fa[] = ;
dfs();
printf("Case %d:\n", cas++);
scanf("%d", &q);
while(q--) {
int u, v;
scanf("%d%d", &u, &v);
lca(u, v);
printf("%d\n", bcnt);
}
printf("\n");
}
return ;
}

poj~3694Network的更多相关文章

  1. POJ 3694Network(Tarjan边双联通分量 + 缩点 + LCA并查集维护)

    [题意]: 有N个结点M条边的图,有Q次操作,每次操作在点x, y之间加一条边,加完E(x, y)后还有几个桥(割边),每次操作会累积,影响下一次操作. [思路]: 先用Tarjan求出一开始总的桥的 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. IDEA怎么生成UML类图

    说之前先说一下Diagram这个单词,意思是图表; 示意图; 图解; [数] 线图的意思. 打开设置 File->Setting或windows下按Ctrl+Alt+S 在搜索框中输入Diagr ...

  2. input属性总结

    <input type="text" readonly="readonly" /> 这个是不能输入的 readonly="readonly ...

  3. php红包算法函数[优化]

    php红包算法 <?php header("Content-Type: text/html;charset=utf-8");//输出不乱码,你懂的 $total=10000; ...

  4. js常用的2中排序方法:冒泡排序和快速排序

    冒泡排序:例如9 4 5 6 8 3 2 7 10 1 首先:9和4比较  4放前   4 9 5 6 8 3 2 7 10 1 4和5比较   4不动   4 9 5 6 8 3 2 7 10 1 ...

  5. 图像的模糊-opencv

    调用两个API,一个是均值模糊,一个是高斯模糊.如下所示: #include<opencv2/opencv.hpp> #include<iostream> using name ...

  6. 3611: [Heoi2014]大工程

    3611: [Heoi2014]大工程 链接 分析: 树形dp+虚树. 首先建立虚树,在虚树上dp. dp:sum[i]为i的子树中所有询问点之间的和.siz[i]为i的子树中有多少询问点,mn[i] ...

  7. IOS多网卡抓包

    linux下libpcap支持从多网卡抓包,设置为any即可 在IOS或者mac上就无法通过次方法抓取所有网卡报文 1.通过设置libevent事件回调,每个网卡注册读事件, fd通过 pd = pc ...

  8. Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏

    其实一种好的UI布局,可以使用户感到更加的亲切与方便.最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧 ...

  9. 自己搭建php服务器(可接受表单提交,并返回页面)

    0.概述 本demo实现以下功能: ①在html页面输入姓名和邮箱,点击提交(这里为get) ②服务器通过解析表单内容,返回对“姓名”和“邮箱”的一个欢迎页面 1.软件准备 ①xampp 作用:提供a ...

  10. 常用模块(sys)

    import sys# sys.argv() # 命令参数List,第一个元素是程序本身路径,如:python test.py run db# sys.exit('shh') # 退出程序,正常退出时 ...