The main problem: There are n points, then give the n points can be connected points.
Question 1: If you want to pass a message to these n points, at least the number of points that need to be passed, and then let these points propagate, so that n points all get information
Question 2: How many edges need to be added to make the N points 22 connected
Problem-solving ideas: Find out all the strong connected components, and then the contraction point, and then the bridge as the path, building the map
Find out that the entry in this figure is 0, because only 0 of the entry is required for notification, and other points can be conveyed through other edges.
How many points need to be added, observe this graph, find out the degrees and degrees of each point, take Max (number of points with zero degrees, number of points with a degree of 0)
Note that when there are only 1 strong connected components,
#include <cstdio>#include <cstring>#define min (a) (a) < (b)? (a): (b))#define Max (a) > (b)? (a): (b))#define N#define M 10010structedge{int from, to, next;} E[M];intHead[n], Sccno[n], stack[n], pre[n], Lowlink[n],inch[N], out[N];intN, tot, Dfs_clock, scc_cnt, top;voidAddedge (intUintV) {E[tot]. from= u; E[tot].to = v; E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {memset (head,-1,sizeof(head)); tot =0;intV for(intU =1; U <= N; u++) { while(SCANF ("%d", &v) && v) addedge (U, v); }}voidDfsintu) {pre[u] = lowlink[u] = ++dfs_clock; Stack[++top] = u;intV for(inti = Head[u]; I! =-1; i = e[i].next) {v = e[i].to;if(!pre[v]) {DFS (v); Lowlink[u] = min (Lowlink[u], lowlink[v]); }Else if(!sccno[v]) {Lowlink[u] = min (Lowlink[u], pre[v]); } }if(Pre[u] = = Lowlink[u]) {scc_cnt++; while(1) {v = stack[top--]; SCCNO[V] = scc_cnt;if(U = = v) Break; } }}voidSolve () {memset (PRE,0,sizeof(pre)); memset (Sccno,0,sizeof(SCCNO)); Dfs_clock = top = SCC_CNT =0; for(inti =1; I <= N; i++)if(!pre[i]) DFS (i);if(scc_cnt = =1) {printf ("1\n0\n");return; } for(inti =1; I <= scc_cnt; i++)inch[I] = out[I] =1;intU, v; for(inti =0; i < tot; i++) {u = sccno[e[i]. from]; v = sccno[e[i].to];if(U! = V) out[U] =inch[V] =0; }intANS1 =0, In_num =0, Out_num =0; for(inti =1; I <= scc_cnt; i++) {if(inch[i]) in_num++;if( out[i]) out_num++; } printf ("%d\n%d\n", In_num, Max (In_num, Out_num));}intMain () { while(SCANF ("%d", &n)! = EOF) {init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ-1236 Network of schools (strongly connected components)