The first should be found to connect components, to shrink points, and then ask for the degree of each component and the degree, the degree of 0 is obviously required to distribute a file, as to the need to add a few sides can become a strong connectivity, is the degree and the most in the degree of the one, because the degree and penetration need to be connected.
#include <cstdio>#include<iostream>#include<cstring>#include<cstring>#include<algorithm>#include<stack>#include<vector>using namespacestd;#defineMAXN 105inthead[maxn],cnt;structedge{intV,next;} E[MAXN*MAXN];intthe Sta[maxn],instack[maxn],dfn[maxn];//sta array Instack array is represented in the stack;inttop,index,low[maxn],belong[maxn],bnt;voidAddintUintv) {E[CNT].V=v; E[cnt].next=Head[u]; Head[u]=cnt++;}voidTarjan (intk) {Dfn[k]=low[k]=++index; INSTACK[K]=1; sta[++top]=K; intJ; for(j=head[k];j!=-1; j=E[j].next) { intv=e[j].v; if(!Dfn[v]) {Tarjan (v); LOW[K]=min (low[k],low[v]); } Else if(Instack[v]) {Low[k]=min (low[k],dfn[v]); } } if(dfn[k]==Low[k]) { ++BNT; Do{J=sta[top--]; INSTACK[J]=0; BELONG[J]=BNT; } while(k!=j); }}voidInit (intN) {CNT=bnt=index=top=0; for(intI=1; i<=n;i++) {Head[i]=-1; Dfn[i]=0; }}intMain () {intN; while(~SCANF ("%d",&N)) {intu,v; Init (n); for(intI=1; i<=n;i++) { while(SCANF ("%d",&v) {Add (i,v); } } for(intI=1; i<=n;i++)// if(!(Dfn[i]) Tarjan (i); intr[maxn]={0},c[maxn]={0},rn=0, cn=0; for(intI=1; i<=n;i++) { for(intj=head[i];j!=-1; j=E[j].next) {u=belong[i],v=Belong[e[j].v];//u is the father of V, so the degree of +1,v of U is +1if(u!=v) {C[u]++; R[V]++; } } } for(intI=1; i<=bnt;i++) { if(r[i]==0) RN++; if(c[i]==0) CN++; } if(bnt==1) printf ("1\n0\n"); Elseprintf ("%d\n%d\n", Rn,max (RN,CN)); } return 0;}
Network of schools POJ 1236