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 ;
}

最新文章

  1. .NET Core RC2/RTM 明确了时间表
  2. WCF学习之旅—基于Fault Contract 的异常处理(十八)
  3. WP8.1 C#代码 添加/获取Grid.ColumnDefinitions/RowDefinitions
  4. JS 4 新特性:混合属性(mixins)之二
  5. git .gitignore 文件 解决二进制文件冲突问题
  6. 接收content-type:multipart/form-data类型的参数
  7. Docker初识
  8. centos 5.x 升级openssl
  9. Collections笔记
  10. ArrayList实现分组功能
  11. MySQL索引优化实例说明
  12. Java经典编程题50道之三
  13. CRMEB 商城系统常见错误修复办法
  14. 从零开始学习Java多线程(二)
  15. cookie路径问题
  16. javascript对样式的操作
  17. mybatis collection使用注意
  18. GuzzleHttp 请求设置超时时间
  19. 使用solr界面管理工具创建core 不能用的解决方法
  20. 02-Maven安装配置

热门文章

  1. 基于Xtrabackup恢复单个innodb表
  2. layer 的功能
  3. 安装破解IDEA(个人使用)
  4. python3 井字棋 GUI - 人机对战、机器对战 (threading、tkinter库)
  5. strak组件(8):基本增删改查实现及应用和排序
  6. 17-比赛2 F - Fox And Two Dots (dfs)
  7. ABAP 7.51 構文書き方変換について
  8. PowerShell技巧:使用XPath语法查询XML文件
  9. 《数据结构与算法分析:C语言描述》复习——第九章“图论”——拓扑排序
  10. Swift 与众不同的地方