NetworkTime
limit:5000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 3694
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 is connected directly or indirectly by successive links, so data can is transformed between any of Computers. The administrator finds that some links is vital to the network, because failure of any one of the them can cause that data C An ' t is 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.
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 the integersN(1≤N≤100,000) andM(N-1≤M≤200,000).
Each of the following M lines contains and integersAandB(1≤A≠B≤N), which indicates a link between computerAandB. Computers is numbered from 1 toN. It is guaranteed, that any, and computers is connected in the initial network.
The next line contains a single integerQ(1≤Q≤1,000), which is the number of the new links the administrator plans to add to the network one by one.
TheI-th Line of the followingQLines contains-integerAandB(1≤A≠B≤N), which is theI-th added new link connecting computerAandB.
The last test was followed by a line containing the zeros.
Output
For each test case, print a line containing the test case number (beginning with 1) and Q lines, the i-t H of which contains a integer indicating the number of bridges in the network after the first I new links is add ed. 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
Main topic: n Points, M edge. Q, ask you to join the new non-edge ui,vi, the figure of the bridge how much more.
Problem-solving ideas: To bridge, Mark the bridge, LCA process to eliminate the bridge.
#include <stdio.h> #include <algorithm> #include <string.h> #include <vector>using namespace std;const int MAXN = 100100;struct edge{int from,to,dist,next; Edge () {} edge (int _to,int _next): to (_to), Next (_next) {}}edges[maxn*4];int dfn[maxn];int fa[maxn],bridge[maxn],dep[ Maxn];int head[maxn];int tot, brinum;int dfs_clock;void init () {tot = 0; Dfs_clock = 0; Brinum = 0; memset (head,-1,sizeof (head)); Memset (Bridge,0,sizeof (bridge)); memset (dfn,0,sizeof (DFN));} void Addedge (int _u,int _v) {edges[tot].to = _v; Edges[tot].next = Head[_u]; Head[_u] = tot++;} int dfs (int u,int f) {int Lowu = dfn[u] = ++dfs_clock; int child = 0; for (int i = head[u]; i =-1; i = edges[i].next) {int v = edges[i].to; if (!dfn[v]) {child++; FA[V] = u; int LOWV = DFS (v,i); Lowu = min (Lowv,lowu); if (Lowv > Dfn[u]) {//Mark Bridge bridge[v] = 1; brinum++; } }else if (Dfn[v] < Dfn[u] && (f^1)! = i) {Lowu = min (lowu,dfn[v]); }}//low[u] = Lowu; return LOWU;} void LCA (int u,int v) {//simplified LCA, reducing the bridge during the LCA process (Dfn[u] < Dfn[v]) {if (bridge[v] = = 1) {BRI num--; BRIDGE[V] = 0; } v = fa[v]; } while (Dfn[u] > Dfn[v]) {if (bridge[u] = = 1) {brinum--; Bridge[u] = 0; } u = Fa[u]; } while (U! = V) {if (Bridge[u]) {brinum--; Bridge[u] = 0; } if (Bridge[v]) {brinum--; BRIDGE[V] = 0; } u = Fa[u]; v = fa[v]; }}int Main () {int n, m, q, cas = 0; while (scanf ("%d%d", &n,&m)!=eof&& (n+m)) {int A, B; Init (); for (int i = 0; i < m; i++) {scanf ("%d%d", &a,&b); Addedge (A, b); Addedge (B,a); } for (int i = 1; I <= n; i++) { if (!dfn[i]) DFS (I,-1); } printf ("Case%d:\n", ++cas); scanf ("%d", &q); for (int i = 0; i < Q; i++) {scanf ("%d%d", &a,&b); LCA (A, b); printf ("%d\n", brinum); }puts (""); } return 0;}
POJ 3694--network —————— "connected graph, LCA for Bridge"