POJ 3694 Tarjan Bridge +LCA

Source: Internet
Author: User

Network
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7298 Accepted: 2651

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≤AB≤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≤ABN), 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

Source

$ Asia Hefei Regional Contest Online by USTC topic meaning: N nodes, M-bar graphs with no edges. Then the Q operation, each operation connects U, v Point, and then outputs the number of remaining bridges in the connected graph. Idea: Mark the bridge first Tarjan, then LCA to remove all the bridges on the path. Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <vector>6#include <queue>7#include <cmath>8#include <Set>9#include <map>Ten#include <stack> One using namespacestd; A  - #defineN 100005 -  the intN, M; - intDfn[n], low[n], time; - intDep[n]; - BOOLBrg[n]; + intFather[n]; - intBrg_num; +  A structedge{ at     intu, V, next; -}e[400005]; -  - intCNT; - intHead[n]; -  in voidSetedge (intUintv) { -e[cnt].u=u;e[cnt].v=v; toe[cnt].next=head[u];head[u]=cnt++; + } -  the voidTarjan (intUintFA) { *     intI, V; $dfn[u]=low[u]=time++;Panax Notoginseng      for(i=head[u];i!=-1; i=E[i].next) { -v=e[i].v; the         if(V==FA)Continue; +         if(!Dfn[v]) { Adep[v]=dep[u]+1; thefather[v]=u; + Tarjan (v,u); -             if(low[v]>Dfn[u]) { $brg_num++; $brg[v]=true; -             } -low[u]=min (low[u],low[v]); the         } -         Elselow[u]=min (dfn[v],low[u]);Wuyi     } the } -  Wu voidLcaintUintv) { -      while(dep[u]>Dep[v]) { About         if(Brg[u]) { $brg[u]=false, brg_num--; -         } -u=Father[u]; -     } A      while(dep[v]>Dep[u]) { +         if(Brg[v]) { thebrg[v]=false, brg_num--; -         } $v=Father[v]; the     } the      while(u!=v) { the         if(Brg[u]) { thebrg[u]=false, brg_num--; -         } inu=Father[u]; the         if(Brg[v]) { thebrg[v]=false, brg_num--; About         } thev=Father[v]; the     } the } +  - Main () the {Bayi     intI, J, K; the     intu, v; the     intKase=1; -      while(SCANF ("%d%d", &n,&m) = =2){ -         if(!n&&!m) Break; thememset (head,-1,sizeof(head)); theCnt=0; the          for(i=0; i<m;i++){ thescanf"%d%d",&u,&v); - Setedge (u,v); the Setedge (v,u); the         } theTime=1;94memset (DFN,0,sizeof(DFN)); thememset (BRG,false,sizeof(BRG)); thebrg_num=0; the          for(i=1; i<=n;i++){98             if(!Dfn[i]) { Aboutdep[i]=0; - Tarjan (i,i);101             }102         }103printf"Case %d:\n", kase++);104         intQ; thescanf"%d",&q);106          for(i=0; i<q;i++){107scanf"%d%d",&u,&v);108 LCA (U,V);109printf"%d\n", brg_num); the         }111cout<<Endl; the     }113}

POJ 3694 Tarjan Bridge +LCA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.