POJ-1236 Network of schools (strongly connected components of a forward graph)

Source: Internet
Author: User
Tags in degrees

D. There is a one-way network between schools, and each school receives a set of software that can be transmitted through a one-way network to the surrounding schools,

Question 1: Initially, at least how many schools need to be distributed software, so that all schools in the network will eventually get the software.

Question 2: At least a few transmission lines (edges) need to be added, so that after any release of software to a school, after several transfers, all schools in the network will eventually get the software.
S.
First, the strong connected component is found, then the penetration of the strong connected component is the total number of 0 points, and the total of the 0 points.

The first question is the number of strongly connected components with a degree of 0.

The second question is that the number of degrees to 0 and the number of degrees to 0 are large. (To find all the subtree of this graph, and then the leaf node of a subtrees tree (out of 0) connected to the root node of another subtrees tree (in degrees 0), so that all the leaf node and root node all eliminated, you can get a whole strong connected components, see the minimum number of edges, This is to see the leaf node and root node which is more, that is, the degree of 0 and into the degree of 0 which is more. Prove? )

C.tarjan

/*Tarjan Algorithm Complexity O (n+m)*/#include<iostream>#include<stdio.h>#include<string.h>using namespacestd;Const intmaxn= the;//pointsConst intmaxm=10100;//Number of sidesstructedge{intTo,next;} EDGE[MAXM];intHead[maxn],tot;intLOW[MAXN],DFN[MAXN],STACK[MAXN],BELONG[MAXN];//the value of the belong array is 1~SCCintIndex,top;intScc//number of strongly connected componentsBOOLINSTACK[MAXN];intNUM[MAXN];//each strong connected component contains the number of points, array number 1~SCC//num arrays do not necessarily need to be combined with actual conditionsvoidAddedge (intUintv) {edge[tot].to=v; Edge[tot].next=Head[u]; Head[u]=tot++;}voidTarjan (intu) {    intv; Low[u]=dfn[u]=++Index; Stack[top++]=T; Instack[u]=true;  for(inti=head[u];i!=-1; i=Edge[i].next) {v=edge[i].to; if(!Dfn[v])            {Tarjan (v); if(Low[u]>low[v]) low[u]=Low[v]; }        Else if(instack[v]&&low[u]>Dfn[v]) Low[u]=Dfn[v]; }    if(low[u]==Dfn[u]) {SCC++;  Do{v=stack[--top]; INSTACK[V]=false; BELONG[V]=SCC; NUM[SCC]++; }         while(v!=u); }}voidSolveintN) {memset (DFN,0,sizeof(DFN)); memset (Instack,false,sizeof(Instack)); memset (num,0,sizeof(num)); Index=scc=top=0;  for(intI=1; i<=n;i++)        if(!Dfn[i]) Tarjan (i);}voidinit () {tot=0; memset (Head,-1,sizeof(head));}intMain () {intN; intv; intINDEGREE[MAXN],OUTDEGREE[MAXN];//The penetration of strongly connected components and the degree of strong connected components    intin0,out0;//the number of strong connected components in the degree of 0, the degree of 0 ...     while(~SCANF ("%d",&N))        {init (); memset (Indegree,0,sizeof(Indegree)); memset (Outdegree,0,sizeof(Outdegree)); In0=out0=0;  for(intu=1; u<=n;++u) {             while(SCANF ("%d",&v)) {                if(v==0) Break;            Addedge (U,V);        }} solve (N); if(scc==1) printf ("1\n0\n");//only one strong connected component        Else{            //enumeration of all the edges, statistics of the penetration of strong connected components, the degree of             for(intI=1; i<=n;++i) {                 for(intj=head[i];j!=-1; j=Edge[j].next) {                    if(belong[i]!=Belong[edge[j].to]) {                        ++Indegree[belong[edge[j].to]]; ++Outdegree[belong[i]]; }                }            }            //The number of the strong connected component is 0, and the number of the degree is 0.             for(intI=1; i<=scc;++i) {                if(indegree[i]==0)++In0; if(outdegree[i]==0)++out0; }            if(in0>out0) printf ("%d\n%d\n", In0,in0); Elseprintf"%d\n%d\n", in0,out0); }    }    return 0;}
View Code

POJ-1236 Network of schools (strongly connected components of a forward graph)

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.