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. rsync+lsyncd实现实时同步

    1.接收端安装rsync,修改/etc/rsyncd.conf配置文件,然后启动服务. uid = rootgid = rootuse chroot = nomax connection = 4sec ...

  2. hadoop-2.0.0-cdh4.1.2升级到hadoop-2.7.2

    升级前准备: 如果是 centos6.x的系统得升级glibc和pam包 在/etc/ld.so.conf 文件里添加 /usr/src/jdk1.6.0_23/jre/lib/amd64/serve ...

  3. js延迟加载的方式有哪些?

    共有:defer和async.动态创建DOM方式(用得最多).按需异步载入js defer属性:(页面load后执行) HTML 4.01 为 <script>标签定义了 defer属性. ...

  4. TP5 行为Behavior用法说明

    TP5 行为Behavior用法说明 无论是tp3还是在tp5中,行为都是一个非常重要的概念,关于太多的理论知识,就不多说了,不了解的请查看开发文档:TP5 行为概述 以下,就由代码来一步一步实现行为 ...

  5. Nginx 配置继承模型

    要了解nginx的继承模型,首先需要知道nginx使用多个配置块进行操作.在nginx中,这样的块被称为上下文,例如,放置在服务器上下文中的配置指令驻留在server { }块中,就像放置在http上 ...

  6. 27-Middleware管道介绍

    1-Middleware管道介绍,. 如果匹配上/task,则界面只会显示i am task. public void Configure(IApplicationBuilder app, IHost ...

  7. python sys模块和序列化模块

    sys模块是与python解释器交互的一个接口: sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0),错误退出sys.exit( ...

  8. Android Studio卡在refreshing gradle project的原因和快速解决办法

    Android Studio更新后一直Refreshing的解决办法! 这个问题遇到过很多次,网上也有很多解决办法,但是好像都没有发现refreshing gradle project在做什么. 一般 ...

  9. Retrofit get post query filed FiledMap

    直接请求型 1.如果是直接请求某一地址,写法如下: @GET("/record") Call getResult(); 2.如果是组合后直接请求,如/result/{id}写法如下 ...

  10. 获取<考试>博文密码!o(*≧▽≦)ツ

    就是CJ高二组通用的密码 如果你想知道,请联系QQ,3057244225,或者直接面对面问博主(...) 是我们的内部材料,原创题目是不能外传的.请谅解. 当然如果是原题的话我们是不会上锁的啦