码迷,mamicode.com
首页 > Web开发 > 详细

poj~3694Network

时间:2018-05-26 00:36:43      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:tween   nbsp   min   rect   find   else   repo   input   transform   

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


图论关于桥的模板

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 using namespace std;
 6 
 7 const int maxn = 2e5 + 10;
 8 int head[maxn], dfn[maxn], vis[maxn], fa[maxn], low[maxn];
 9 int dcnt, bcnt, tot;
10 struct node {
11     int v, next, used;
12 } edge[4 * maxn] ;
13 int isbridge[maxn];
14 void add(int u, int v) {
15     edge[tot].v = v;
16     edge[tot].next = head[u];
17     edge[tot].used = 0;
18     head[u] = tot++;
19 }
20 void lca(int u, int v) {
21     if (dfn[u] < dfn[v]) swap(u, v);
22     while(dfn[u] > dfn[v]) {
23         if(isbridge[u]) bcnt--;
24         isbridge[u] = 0;
25         u = fa[u];
26     }
27     while(u != v) {
28         if (isbridge[u]) bcnt--;
29         if (isbridge[v]) bcnt--;
30         isbridge[u] = isbridge[v] = 0;
31         u = fa[u], v = fa[v];
32     }
33 }
34 void dfs(int u) {
35     vis[u] = 1;
36     dfn[u] = low[u] = ++dcnt;
37     for (int i = head[u]; i != -1 ; i = edge[i].next )
38         if (!edge[i].used) {
39             edge[i].used = edge[i ^ 1].used = 1;
40             int v = edge[i].v;
41             if (!vis[v]) {
42                 fa[v] = u;
43                 dfs(v);
44                 low[u] = min(low[u], low[v]);
45                 if (dfn[u] < low[v]) {
46                     bcnt++;
47                     isbridge[v] = 1;
48                 }
49             } else if (vis[v] == 1) low[u] = min(low[u], dfn[v]);
50         }
51     vis[u] = 2;
52 }
53 void init() {
54     tot = dcnt = bcnt = 0;
55     memset(head, -1, sizeof(head));
56     memset(isbridge, 0, sizeof(0));
57     memset(vis, 0, sizeof(head));
58 }
59 int main() {
60     int cas = 1, n, m, q;
61     while(scanf("%d%d", &n, &m) != EOF) {
62         if (n == 0 && m == 0 ) break;
63         init();
64         for (int i = 0 ; i < m ; i++) {
65             int u, v;
66             scanf("%d%d", &u, &v);
67             add(u, v);
68             add(v, u);
69         }
70         fa[1] = 1;
71         dfs(1);
72         printf("Case %d:\n", cas++);
73         scanf("%d", &q);
74         while(q--) {
75             int u, v;
76             scanf("%d%d", &u, &v);
77             lca(u, v);
78             printf("%d\n", bcnt);
79         }
80         printf("\n");
81     }
82     return 0;
83 }

 

poj~3694Network

标签:tween   nbsp   min   rect   find   else   repo   input   transform   

原文地址:https://www.cnblogs.com/qldabiaoge/p/9091118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!