Poj 3694 (Bridge problem + LCA problem)

Source: Internet
Author: User
Tags idn

Network
Time limit:5000 Ms   Memory limit:65536 K
Total submissions:3868   Accepted:1321

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) andM(N-1 ≤M≤ 200,000 ).
Each of the following M lines contains two integersAAndB(1 ≤A=B≤ N), which indicates a link between computerAAndB. Computers are numbered from 1N. It is guaranteed that any two computers
Are connected in the initial network.
The next line contains a single integerQ(1 ≤Q≤ 1,000), which is the number of new links the administrator plans to add to the network one by one.
TheI-Th line of the followingQLines contains two integerAAndB(1 ≤A=BN), Which isI-Th added new link connecting computerAAndB.

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) andQLines,I-Th of which contains a integer indicating the number of bridges in the network after the firstINew 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

2008 Asia Hefei Regional Contest online by USTC question: http://poj.org/problem? Id = 3694 analysis: At the beginning, I thought of finding a bridge and then solved it with LCA (the smallest common ancestor, I have been trying to make a simple practice, but I still haven't made it. There are two common practices: the first one is better understanding, and the efficiency is relatively high. Generally, all edge connected blocks are obtained, scale it into a vertex so that the source image becomes a tree. Each time you ask, you can find the new vertex corresponding to the two vertices. Then, you can see that there are several edges between them and their LCA, reduce the total variable-s, and then scale all the three vertices directly into one point. This is a true implementation .... Second, we have layered the graph while finding the bridge. We don't need to construct a new graph. We just need to do it in the source image according to the theorem, record the vertex that belongs to the cut edge (which is then traversed), and the reverse edge of all vertices (that is, traversing through that edge). In this way, each time you ask two points, first, move them to the same layer, and then determine whether the two points are verbose. If the two points are not moved up at the same time until the verbose points are reached, if a point marked as a bridge is encountered during the moving process, subtract one from the answer, change the flag... The second solution is lucky. If there is a set of data in the question that makes the graph a straight line, and then the complexity is upgraded to (QV) each time the query header and tail appear ), it's terrible ~~~~ Make up the first writing method tomorrow. Well, I actually thought about a method similar to and query the set yesterday to optimize the second one. However, I am frustrated. Today I have read it carefully and it turns out to be a low-level error... As long as you record the points in the path when you check the LCA, and change all their parent nodes to the LCA, the time is 290 + + MS.Code (the red part is the modified part ):
# Include <cstdio> # define min (a, B) (a <B? A: B) using namespace STD; const int Mm = 444444; const int Mn = 111111; int T [mm], p [mm]; int H [Mn], pre [Mn], dfn [Mn], low [Mn]; bool BG [Mn]; int I, j, k, n, m, Q, top, IDN, sum, CAS = 0; void DFS (int u) {dfn [u] = low [u] = ++ IDN; For (INT I = H [u], V; i> = 0; I = P [I]) if (! Dfn [V = T [I]) {pre [v] = u, DFS (V); low [u] = min (low [u], low [v]); If (dfn [u] <low [v]) ++ sum, BG [v] = 1;} else if (V! = Pre [u]) low [u] = min (low [u], dfn [v]);} void Tarjan () {for (INT I = 0; I <= N; ++ I) dfn [I] = BG [I] = 0; IDN = sum = 0, DFS (1);} void move (Int & X) {If (BG [x]) -- sum, BG [x] = 0; P [top ++] = (x = pre [x]); // record node} int LCA (int x, int y) {P [0] = x, P [1] = Y, Top = 2; // initialize the stack while (X! = Y) {If (dfn [x]> dfn [y]) Move (x); else if (dfn [x] <dfn [y]) Move (y ); else move (x), move (y);} while (top --) if (P [Top]! = X) Pre [p [Top] = x; // change the parent node return sum;} int main () {While (scanf ("% d ", & N, & M), N + M) {for (I = k = 0; I <= N; ++ I) H [I] =-1; while (M --) {scanf ("% d", & I, & J); t [k] = J, P [k] = H [I], H [I] = K ++; t [k] = I, P [k] = H [J], H [J] = K ++;} Tarjan (); scanf ("% d", & Q); printf ("case % d: \ n", ++ CAS); While (Q --) {scanf ("% d", & I, & J); printf ("% d \ n", LCA (I, j ));} puts ("");} return 0 ;}

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.