A number of schools is connected to a computer network. Agreements has been developed among those Schools:each School maintains a list of schools to which it distributes Softwa Re (the "Receiving schools"). Note that if B was in the distribution list of school A, then a does not necessarily appear in the list of school B
You is to write a program this computes the minimal number of schools that must receive a copy of the new software in Ord Er for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure is sending the copy of new software to an arbitrary school, this software would r Each of the schools in the network. To achieve this goal we are having to extend the lists of receivers by new members. Compute the minimal number of extensions that has to is made so this whatever school we send the new software to, it'll Reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
Input
The first line contains a integer n:the number of schools in the network (2 <= N <= 100). The schools is identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school I. Each list is ends with a 0. An empty list contains a 0 alone in the line.
Output
Your program should write, lines to the standard output. The first line should contain one positive integer:the solution of subtask A. The second line should contain the solution of subtask B.
Sample Input
52 4 3 04 5 0001 0
Sample Output
12
1, a graph, ask at least select a few points to do the source, so that the point can be reached by them (a point can reach any number of post-order points)------to find the degree of 0 points. 2, a direction diagram, ask at least a few edges after strong connectivity------to max (in the degree of 0 points, the number of degrees of 0), previously proved. However, it is important to note that there is an error in the case of strong connectivity (requires a special sentence). 3, a direction graph, ask at least select a few points to do the source, so that you can reach through them so point (a point can reach a post-order point)------The minimum path cover after the indentation (two-figure matching) This problem only 1, 22 questions, we must pay attention to the Special award.
//there is a point to the graph, note when scc_cnt=1. #include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn= About;intlaxt[maxn],next[maxn* $],to[maxn* $],cnt,n;intDFN[MAXN],LOW[MAXN],TIMES,SCC_CNT,SCC[MAXN];intInstc[maxn],stc[maxn],top,ans1,ans2;intIND[MAXN],OUD[MAXN];voidUpdate () {CNT=times=scc_cnt=top=ans1=ans2=0; memset (Laxt,0,sizeof(LAXT)); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (SCC,0,sizeof(SCC)); memset (INSTC,0,sizeof(INSTC)); memset (STC,0,sizeof(STC)); memset (Ind,0,sizeof(Ind)); memset (Oud,0,sizeof(Oud));}voidAddintUintv) {next[++cnt]=Laxt[u]; Laxt[u]=CNT; TO[CNT]=v;}voidDfsintu) {Dfn[u]=low[u]=++Times ; stc[++top]=u; instc[u]=1; for(intI=laxt[u];i;i=Next[i]) { intv=To[i]; if(!Dfn[v]) {DFS (v); Low[u]=min (low[u],low[v]); } Else if(Instc[v]) {Low[u]=min (low[u],dfn[v]); } } if(dfn[u]==Low[u]) {scc_cnt++; while(true){ intx=stc[top--]; SCC[X]=scc_cnt; INSTC[X]=0; if(X==u) Break; } }}voidTarjan () { for(intI=1; i<=n;i++) if(!Dfn[i]) DFS (i); for(intI=1; i<=n;i++) for(intj=laxt[i];j;j=Next[j]) { if(scc[i]!=Scc[to[j]]) {Ind[scc[to[j] ]++; Oud[scc[i]]++; } } for(intI=1; i<=scc_cnt;i++){ if(ind[i]==0) ans1++; if(oud[i]==0) ans2++; }}intMain () { while(~SCANF ("%d",&N)) {update (); for(intI=1; i<=n;i++) { intX while(SCANF ("%d",&x)) { if(x==0) Break; Add (i,x); }} Tarjan (); if(scc_cnt==1) printf ("1\n0\n"); Elseprintf"%d\n%d\n", ANS1,ANS2); } return 0;}
POJ1236 Network of schools (strong connected component, note boundary)