Topic Links:
Poj 1236 Network of schools
Title Description:
There are n schools, and schools have some one-way routes for transmitting radios, and when a school gets a network that can route to other schools through the Internet, 1: At least a few networks are allocated to allow all schools to be covered by the Internet? 2: At least add a few lines to be able to install the network in any school can cover all schools?
Problem Solving Ideas:
First use the Tarjan to the strong connected components of the indentation, and then to the point after the reduction of the graph to deal with, the number of nodes in the statistical graph is zero, the degree of zero is how many?
Because points with zero-in degrees cannot be reached by other points, installing a network on each node with a zero-in degree allows all schools to be covered by the network.
All schools in a strong connected diagram, you can achieve the second question, because the zero-out node can not reach the other nodes, the point of zero in degrees can not be entered by other nodes, so to the degree of zero plus degrees, the degree of zero in the degree can be added. PS: When the whole map has only one connected block, it should not be added side.
1#include <cstring>2#include <cstdio>3#include <string>4#include <algorithm>5 using namespacestd;6 Const intMAXN = the;7 structnode8 {9 intto, next;Ten} edge[maxn*MAXN]; One intHEAD[MAXN], DFN[MAXN], LOW[MAXN],inch[MAXN], out[MAXN], ID[MAXN]; A inttot, CNT, top, time, INSTACK[MAXN], STACK[MAXN]; - voidInit () - { thetot = top =0; -CNT = time =0; -Memset (Head,-1,sizeof(head)); -memset (DFN,0,sizeof(DFN)); +memset (Low,0,sizeof(Low)); -memset (inch,0,sizeof(inch)); +memset ( out,0,sizeof( out)); Amemset (ID,0,sizeof(ID)); atmemset (Instack,0,sizeof(Instack)); -memset (Stack,0,sizeof(stack)); - } - voidADD (int from,intto ) - { -Edge[tot].to =to ; inEdge[tot].next = head[ from]; -head[ from] = tot + +; to } + voidTarjan (intu) - { theDfn[u] = Low[u] = + +Time ; *Instack[u] =1; $Stack[top + +] =u;Panax Notoginseng for(intI=head[u]; i!=-1; I=edge[i].next) - { the intv =edge[i].to; + if(!Dfn[v]) A Tarjan (v); the if(Instack[v]) +Low[u] =min (Low[u], low[v]); - } $ if(Dfn[u] = =Low[u]) $ { -CNT + +; - while(1) the { - intv = stack[--top];WuyiINSTACK[V] =0; theID[V] =CNT; - if(v = =u) Wu Break; - } About } $ } - intMain () - { - intN; A while(SCANF ("%d", &n)! =EOF) + { the init (); - for(intI=1; i<=n; i++) $ { the intto ; the while(SCANF ("%d", &to ) the Add (I, to); the } - for(intI=1; i<=n; i++) in if(!Dfn[i]) the Tarjan (i); the for(intI=1; i<=n; i++) About for(intJ=head[i]; j!=-1; j=edge[j].next) the { the intv =Id[i]; the intU =id[edge[j].to]; + if(V! =u) - { the out[V] + +;Bayi inch[U] + +; the } the } - intin, out; -in = out =0; the for(intI=1; i<=cnt; i++) the { the if(!inch[i]) theIn + +; - if(! out[i]) theOut + +; the } the if(CNT = =1)94printf ("1\n0\n"); the Else theprintf ("%d\n%d\n", IN, Max (in, out)); the }98 return 0; About}
Poj 1236 Network of schools (Tarjan)