POJ1236 (Network of Schools,tarjan, strongly connected components)

Source: Internet
Author: User

Reprinted from: Http://www.tuicool.com/articles/EnMFFja

Original: Kuangbin

Test instructions

  A direction graph, ask:

1) Select at least a few vertices to be able to proceed from these vertices to reach all vertices

2) At least how many edges must be added to make it possible to reach all vertices from any vertex, i.e. become a strong connected component

Ideas:

After the strong connected component is obtained, the indentation point is calculated, and the degree of each point is computed.

The answer to the first question is the number of points with zero degrees,

The second question is that Max (n,m) is zero in the number of N, the number of degrees zero is m

Detailed ideas:

     1. Find all the strongly connected components      2. Each strongly connected component is shrunk to a point, forming a directed acyclic graph Dag.      3. The number of vertices in the dag above the 0, the answer to question 1 is how many sides on the Dag to make the Dag become strong, the answer to question 2 is how many plus edges: To add an edge for each point with a degree of 0, and to add an edge for each point with a degree of 0, assume that there is n a point with an entry degree of 0 and a point with a degree of 0 for M, how to add edge? Number of points with all degrees 0 0,1,2,3,4 .... N-1 each time for a number I in the degree of 0 points can reach the out of 0 points, add an out edge, connected to the number (i+1)%N that out of 0 points, which need to add n edge if M <= N, then add this n edge, has no degree 0 points, then the problem solved, altogether added N edge if M > N, Then there are m-n 0 points, then take a point from outside these points, and these points are connected to the top, it is necessary to add m-n edge. So, Max (M,n) is the solution to the second problem. In addition: when there is only one strong connected branch, that is, only one point after the indentation, although the degree of 0 has a degree of one, but actually do not need to add the list of items, so the answer is 1,0; 

Tarjan Algorithm Shallow Solution:

1. Process:

(1) Dfn[i] represents the timestamp of vertex i when DFS is reached

Low[i] Indicates that I can reach the smallest vertex directly or indirectly (e.g. 3->1 low[3] = 1)

(2) time is initialized to 0, low[u]=dfn[u]=time++,u into the stack when dfs traverses to u

< Span lang= "en-us" xml:lang= "en-US" > scan U's adjacency vertex v if v

< Span lang= "en-us" xml:lang= "en-US" > Low[u]=min (Low[u],low[v])

< Span lang= "en-us" xml:lang= "en-US" >       If the adjacency vertex has been scanned at this time: i.e. Dfn[v]! = 0 and &NBSP;BELONG[V] = = 0,  low[u] = min (low[u],dfn[v]);

If Low[v]=dfn[v], the vertices of the stack V and above are all out of the stack, and just out of the stack is a strongly connected component.

2. Approximate principle:

Pits ...

Code:

1 //#include <bits/stdc++.h>2#include <vector>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <stack>7 using namespacestd;8typedefLong Longll;9 Ten Const intMAXN = -+Ten; One  Avector<int>G[MAXN]; - intn,dfn[maxn],low[maxn],belong[maxn],dfs_clock,scc_cnt; -stack<int>S; the  - /** -  - Tarjan Algorithm +  - */ +  A voidDfsintu) { atDfn[u] = Low[u] = + +Dfs_clock; - s.push (u); -      for(inti =0; I < g[u].size (); i + +){ -         intv =G[u][i]; -         if(!Dfn[v]) { - Dfs (v); inLow[u] =min (low[u],low[v]); -}Else if(!Belong[v]) { toLow[u] =min (low[u],dfn[v]); +         } -     } the     if(Low[u] = =Dfn[u]) { *scc_cnt++; $          for(;;) {Panax Notoginseng             intx =S.top (); S.pop (); -BELONG[X] =scc_cnt; the             if(x = = u) Break; +         } A     } the } +  - voidSccintN) { $Dfs_clock = scc_cnt =0; $memset (DFN,0,sizeof(DFN)); -Memset (Belong,0,sizeof(belong)); -      for(inti =0; I < n; i + +) the         if(!Dfn[i]) DFS (i); - Wuyi } the  - intMain () { Wu     intx; -      while(SCANF ("%d", &n)! = EOF &&N) { About          for(inti =0; I <= N; i + +) g[i].clear (); $          for(inti =0; I < n; i + +){ -              while(SCANF ("%d",&x), x) { ---x; - g[i].push_back (x); A             } +         } the SCC (n); -         if(scc_cnt = =1){ $printf"1\n0\n"); the             Continue; the         } the         int inch[MAXN], out[Maxn],in_tot =0, Out_tot =0; theMemsetinch,0,sizeof(inch)); -Memset out,0,sizeof( out)); in          for(inti =0; I < n; i + +){ the              for(intj =0; J < G[i].size (); J + +){ the                 intv =G[i][j]; About /** the iterate through each point. the traverse the point connected to the point the determine if two points are the same connected component + if not, change the degree of access after the indent. -  the */Bayi                 if(Belong[i]! =Belong[v]) { the                      out[Belong[i]] + +; the                     inch[Belong[v]] + +; -                 } -             } the         } the          for(inti =1; I <= scc_cnt; i + +){ the             if(!inch[i]) in_tot++; the             if(! out[i]) out_tot++; -         } the  theprintf"%d\n%d\n", In_tot,max (In_tot,out_tot)); the     }94     return 0; the}

POJ1236 (Network of Schools,tarjan, strongly connected components)

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.