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. cordforce 495 补题 <未完>

    题目链接: http://codeforces.com/contest/1004/my A. Sonya and Hotels 分类讨论 看第一个样例解释的时候没看到后面第二行还有一个19,想了半天为 ...

  2. scrapy--selenium

    一直在学习scrapy的爬虫知识,但是遇到了动态加载页面的难题,从一开始的javascript渲染器--splash,docker服务, 遇到各种奇葩的问题: 1.docker代理设置添加无效,导致无 ...

  3. php-5.6.26源代码 - PHP文件汇编成opcode(require、include的差异)

    文件 php-5.6.26/Zend/zend_language_scanner.c ZEND_API zend_op_array *compile_file(zend_file_handle *fi ...

  4. 7,Flask 中路由系统

    Flask中的路由系统 @app.route("/",methods=["GET","POST"]) 为什么要这么用?其中的工作原理我们知道 ...

  5. WPF点击不同界面上的按钮实现界面切换

    原文:WPF点击不同界面上的按钮实现界面切换 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_29844879/article/details/ ...

  6. 【tomacat集群】Linux或 window配置多个Tomcat同时运行-完美解决-未来星开发团队-费元星

    Linux系统下怎样配置多个Tomcat同时运行呢,首先修改变量为第一个tomcat,然后修改第二个tomcat启动的脚本 如何在同一系统里同时启动多个Tomcat    http://www.cnb ...

  7. CentOS 7 安装Oracle 11gR2

    概述 Oracle 在Linux和window上的安装不太一样,公司又是Linux系统上的Oracle,实在没辙,研究下Linux下Oracle的使用,oracle默认不支持CentOS系统安装,所以 ...

  8. <<程序猿健康指南>> 笔记

    序言: 长时间对着电脑工 作.一天下来基本不怎么走动的人,患高血压及 2 型糖尿病的风险远高于其他人群, 这两种疾病会对人体的健康产生长久的严重影响,还会增加心脏病及中风的几率. 前言: 阿米什人(A ...

  9. 《Cracking the Coding Interview》——第6章:智力题——题目1

    2014-03-19 06:40 题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药.给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的? 解法:如果药片管 ...

  10. 基类VS接口

    该篇引用 CLR via C# 中的13.11节. 应该设计基类还是接口,这个问题不能一概而论,下面提供一些指导性原则: 1. IS_A关系(指属于,例如汽车属于交通工具) vs CAN_DO关系(指 ...