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. Spring MVC 接收前端参数的方式

    方式一: 普通方式接收 1 @RequestMapping("/index") 2 public String getUserName(String username) { 3 S ...

  2. centos 7忘记了root密码,如何改密码?

    今天服务器突然进不去了,不知道是密码被改了,还是什么情况! 服务器版本:centos 7.0 网上查找了很多文档,有些办法不可行,如果是亲自试过绝对可行的方法: 1:重启服务器,如下界面,按键盘 &q ...

  3. openldap完整版本搭建记录

    文档信息 目        的:搭建一套完整的OpenLDAP系统,实现账号的统一管理.                     1:OpenLDAP服务端的搭建                   ...

  4. TP5 行为Behavior用法说明

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

  5. 详解JavaScript中的arc的方法

    今天说说JavaScript在网页中画圆的函数arc! 一.arc所需要的参数设置 1 arc(x, y, radius, startAngle, endAngle, counterclockwise ...

  6. 右键git-bash不能使用

    主要:右键git-bash不能使用 右键git-bash不能使用 今日想用git传写代码到仓库,突然发现几天没有却出问题了,右键出现了错误,虽然很快解决了,但还是可以记录下 情形: 右键存在但不能使用 ...

  7. python中的文件操作小结1

    #!/usr/local/bin/python3 # -*- coding:utf-8 -*- f=open("test_1",'r',encoding="utf-8&q ...

  8. opencv中对图像的像素操作

    1.对灰度图像的像素操作: #include<iostream> #include<opencv2/opencv.hpp> using namespace std; using ...

  9. [CodeForces950C]Zebras

    Description 题目地址: Codeforces 题意:给你一串只含01的字符串,判断能否将字符串分为k个子序列,使得子序列满足以下条件: 开头和结尾都是0 相邻的2个数是01或者10 如0, ...

  10. Mplab X IDE 安装DMCI

     DMCI在Mplab 8中是默认安装的,在 Mplab X IDE中是作为插件,默认不安装.   找到     勾选前面的复选框,点击安装