Constructing strong connected graphs

Source: Internet
Author: User

  

We know that to construct a non-edge double connected graph into a two-side connected graph, we just need to make the graph simple (the edge of the two connected components) into a tree, and then find the leaf node number leaf, (leaf+1)/2 is the number of new edges to add.

Now, to the graph, we need to add the least edge, so that a non-strong connected graph into a strong connected graph, the minimum number of edges?

Similarly, we need to reduce the number of vertices (strongly connected components) into a DAG graph (directed acyclic graph), and then find the number of vertex in with a degree of 0, and a vertex with a degree of 0 out, whichever is the minimum number of edges to add.

For a special case, the Wakahara diagram itself is a strong connected graph, then the DGA graph after the contraction point is an isolated point, in and out is equal to 1, the answer is 1 according to the above method, obviously and the correct answer 0 is different, so, we have a special sentence here.

POJ 1236

1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 #define_CLR (x, y) memset (x, y, sizeof (x))6 #defineINF 0x3f3f3f3f7 #defineN 1108 using namespacestd;9 Ten structNode One { A     intto, next; -}edge[n* -]; - inthead[n], tot; the intDfn[n], low[n]; - intSta[n], bleg[n]; - BOOLInstack[n]; - intN, CNT, top, ght; +  - voidInit () + { Acnt=tot=top=ght=0; at_CLR (Head,-1); -_CLR (DFN,0); -_CLR (Instack,0); - } -  - voidAdd_edge (intAintb) in { -Edge[tot].to =b; toEdge[tot].next =Head[a]; +Head[a] = tot++; - } the  * voidDfsintu) $ {Panax Notoginsengdfn[u]=low[u]=++CNT; -Instack[u] =true; thesta[top++] =u; +      for(intI=head[u]; i!=-1; I=edge[i].next) A     { the         intv =edge[i].to; +         if(!Dfn[v]) -         { $ Dfs (v); $Low[u] =min (Low[u], low[v]); -         } -         Else if(Instack[v]) low[u] =min (Low[u], dfn[v]); the     } -     if(low[u]==Dfn[u])Wuyi     { theght++; -   //printf ("num:%d\n", ght); Wu         intv; -          Do About         { $v = sta[--top]; -     //printf ("%d", v); -INSTACK[V] =false; -BLEG[V] =ght; A} while(U! =v); +       //puts (""); the     } - } $  the intInde[n], outde[n]; the voidTarjan () the { the      for(intI=1; i<=n; i++) -         if(!Dfn[i]) DFS (i); in  the_CLR (Inde,0); the_CLR (OUTDE,0); About      for(intu=1; u<=n; u++) the      for(intI=head[u]; i!=-1; I=edge[i].next) the     { the         intv =edge[i].to; +         if(Bleg[v]! =Bleg[u]) -inde[bleg[v]]++, outde[bleg[u]]++; the     }Bayi } the  the voidSolved () - { -     intA=0, b=0; the     if(ght==1) theprintf"1\n0\n"); the     Else the     { -          for(intI=1; i<=ght; i++) the         { the             if(inde[i]==0) a++; the             if(outde[i]==0) b++;94         } theprintf"%d\n%d\n", A, Max (A, b)); the     } the }98 intMain () About { -     intb;101      while(~SCANF ("%d", &N))102     {103 Init ();104          for(intI=1; i<=n; i++) the         {106              while(SCANF ("%d", &b) &&b)107 Add_edge (i, b);108         }109 Tarjan (); the Solved ();111     } the     return 0;113}
View Code

Constructing strong connected graphs

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.