POJ 1236 Network of schools (Tarjan algorithm + LCA)

Source: Internet
Author: User

This topic online has many answers, the code is also very similar, does not exclude me. Everyone's idea should be taijan to find the cutting edge, and then look for two points of the LCA (the recent public ancestor), these two points and LCA and other points constitute a ring, we judge the ring on the edge of a few, we have fewer answers.

Some people ask, this topic heavy side how to do it, heavy side certainly not bridge ah. The amount of ... For this I can only say that the original picture of the problem should be no heavy edges, and then the side may have a heavy edge, but does not affect our judgment, because we have to mark the way to the point of the sentence. (This problem should be explained, ah, the problem is a bit ...) )。

Second, the fun of the problem came, the online people to share the code, incredibly not a set of very simple sample (as follows), (now most people may have been fixed), in fact, this is the reason I should be found, that is DFN (date of birth) and deep (depth search layer) of the use of errors, When you use deep to exchange the values of the two is correct, because we always let them adjust to the first layer, and then their LCA must be on the first layer, using the FA array can be traced back to their LCA. However, it is wrong to exchange the values of the two with DFN, because even if you adjust their values to a certain size, they are not necessarily on the first floor, they are likely to go back to the outside of the ring, so it is likely to be more than a few sides in the trace. That is, the following example appears, the answer is 1, someone output 0;

In fact, this place I have always had a doubt, that is the use of DFN judgment, the last time actually will appear the two unequal situation, this and the nature of the deep search against, and now did not find a sample can show that he is right, I do feel that when the first two cycles of judgment DFN "U" Is their LCA, this doubts I put here first, easy to answer later, in the future I will mainly choose to record the depth of the method to do, better understanding, more insurance (as I have in the code below)

Attached: This problem also has a solution, is to find the two connected components, by judging whether two points in a component to determine whether the edge between two points is a bridge, and then merge the two points. One way to find the connected component is to check the set, merge it into a set when judging, one is the storage way of the stack, the ID array, and the method to find the strong connected component of the graph is consistent. Note that the dual-connected components of undirected graphs do not mean that they can reach each other, but that the component does not contain a bridge, so when two points are not in a set, this edge must be the bridge.

6 5
0 S
2 3
2 4
3 5
4 6
1
5 6

Answer: 1

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;#defineMAXN 100010intHEAD[MAXN],DFN[MAXN],LOW[MAXN],FA[MAXN],DEEP[MAXN];intAll,tot,bridge[maxn],brinum;structedge{intTO,NXT;} edge[4*MAXN];voidAddadge (intAintb) {edge[tot].to=b; EDGE[TOT].NXT=Head[a]; Head[a]= tot++;}voidDfsintUintPA) {Low[u]= Dfn[u] = + +All ; Deep[u]= Deep[pa] +1;  for(inti = Head[u]; I! =-1; i =edge[i].nxt) {        intv =edge[i].to; if(!Dfn[v]) {Fa[v]=u;            DFS (V,U); Low[u]=min (low[u],low[v]); if(Low[v] >Dfn[u]) {Bridge[v]=1; Brinum++; }        }        Else if(v! = PA) Low[u] =min (low[u],dfn[v]); }}voidLcaintAintb) {     while(Deep[a] >Deep[b]) {        if(Bridge[a]) {Bridge[a]=0; Brinum--; } A=Fa[a]; }     while(Deep[b] >Deep[a]) {        if(Bridge[b]) {Bridge[b]=0; Brinum--; } b=Fa[b]; }     while(A! =b) {if(Bridge[a]) {Bridge[a]=0; Brinum--; }        if(Bridge[b]) {Bridge[b]=0; Brinum--; } A=Fa[a]; b=Fa[b]; }}intMain () {intN,m,q,a,b,ca =0;  while(~SCANF ("%d%d",&n,&m)) {if(!n &&!m) Break;  for(inti =0; I <= N; i++) {Head[i]= -1; Dfn[i]=0; Low[i]=0; Fa[i]=i; Deep[i]=0; Bridge[i]=0; } tot=0; All=0; Brinum=0;  for(inti =0; I < m; i++) {scanf ("%d%d",&a,&b);            Addadge (A, b);        Addadge (B,a); } DFS (1,1); scanf ("%d",&q); printf ("Case %d:\n",++CA);  while(q--) {scanf ("%d%d",&a,&b);            LCA (A, b); printf ("%d\n", Brinum); } puts (""); }    return 0;}

POJ 1236 Network of schools (Tarjan algorithm + 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.