POJ3694----Network

來源:互聯網
上載者:User

標籤:

Network
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6989   Accepted: 2497

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 integersN(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 andB. 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) andQ lines, the i-th of which contains a integer indicating the number of bridges in the network after the firsti new links are added. Print a blank line after the output for each test case.

Sample Input

3 21 22 321 21 34 41 22 12 31 421 23 40 0

Sample Output

Case 1:10Case 2:20

Source


先tarjan,縮點得到樹,然後暴力LCA+並查集統計橋的數目


/*************************************************************************    > File Name: tj5.cpp    > Author: ALex    > Mail: [email protected]     > Created Time: 2015年01月17日 星期六 13時17分51秒 ************************************************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;const int N = 110010;const int M = 210010;int head[N];int head2[N];int DFN[N];int low[N];int deep[N];int father[N];int block[N];int pre[N];int st[N];bool instack[N];int tot, tot2, top, ord, sccnum, icase;struct node{int next;int to;int id;}edge[M << 2], edge2[M << 2];void init (){memset (DFN, -1, sizeof(DFN));memset (head, -1, sizeof(head));memset (head2, -1, sizeof(head2));memset (low, 0, sizeof(low));memset (instack, 0, sizeof(instack));tot = tot2 = ord = sccnum = top = 0;}void addedge (int from, int to, int id){edge[tot].to = to;edge[tot].id = id;edge[tot].next = head[from];head[from] = tot++;}void addedge2 (int from, int to){edge2[tot2].to = to;edge2[tot2].next = head2[from];head2[from ] = tot2++;}void dfs (int u, int fa, int d){pre[u] = fa;deep[u] = d;for (int i = head2[u]; ~i; i = edge2[i].next){int v = edge2[i].to;if (v == fa){continue;}dfs (v, u, d + 1);}}int find (int x){if (x == father[x]){return x;}return father[x] = find(father[x]);}void tarjan (int u, int x){DFN[u] = low[u] = ++ord;instack[u] = 1;st[top++] = u;for (int i = head[u]; ~i; i = edge[i].next){int v = edge[i].to;if (edge[i].id == x){continue;}if (DFN[v] == -1){tarjan (v, edge[i].id);low[u] = min(low[u], low[v]);}else if (instack[v]){low[u] = min(low[u], DFN[v]);}}int v;if (DFN[u] == low[u]){++sccnum;do{v = st[--top];instack[v] = 0;block[v] = sccnum;}while (v != u);}}int LCA (int a, int b){while (a != b){if (deep[a] > deep[b]){a = pre[a];}else if (deep[a] < deep[b]){b = pre[b];}else{a = pre[a];b = pre[b];}a = find(a);b = find(b);}return a;}void solve (int n){tarjan (1, -1);for (int u = 1; u <= n; ++u){for (int i = head[u]; ~i; i = edge[i].next){int v = edge[i].to;if (block[u] == block[v]){continue;}addedge2 (block[u], block[v]);}}for (int i = 1; i <= sccnum; ++i){father[i] = i;}int cnt = sccnum - 1;dfs(1, -1, 0);int q, a, b, lca;scanf("%d", &q);printf("Case %d:\n", icase++);while (q--){scanf("%d%d", &a, &b);a = block[a];b = block[b];if (a == b){printf("%d\n", cnt);continue;}a = find(a);b = find(b);lca = LCA (a, b);int x = 0;while (a != lca){++x;father[a] = lca;a = pre[a];a = find(a);}while (b != lca){++x;father[b] = lca;b = pre[b];b = find(b);}cnt -= x;printf("%d\n", cnt);}}int main(){int n, m;int u, v;icase = 1;while (~scanf("%d%d", &n, &m)){if (!n && !m){break;}init();for (int i = 1; i <= m; ++i){scanf("%d%d", &u, &v);addedge (u, v, i);addedge (v, u, i);}solve(n);printf("\n");}return 0;}


POJ3694----Network

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.