POJ 1236 Network of schools (strong connected components)

Source: Internet
Author: User

Title Address: POJ 1236

The main idea of this question is to ask at least how many points to send the message can make any one point can receive the message and the minimum number of additional edges can make the graph a connected graph. For the first problem, you can find the number of strong connected blocks with a degree of 0, because only a strong connected block with a degree of 0 is unable to receive information from the outside world, and as long as there is an entry, then the entire connected block can receive information. The second problem is the maximum of the number of strongly connected blocks with a degree of 0 and a strong connected block with a degree of 0.

The code is as follows:

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include < stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include < Set> #include <algorithm>using namespace Std;int head[200], CNT, index, top, ans, cntin, Cntout, in[200], out[200] int dfn[200], belong[200], instack[200], stak[200], low[200];struct node{int u, V, next;} edge[100000];void Add (int u    , int v) {edge[cnt].v=v;    Edge[cnt].next=head[u]; head[u]=cnt++;}    void Init () {memset (head,-1,sizeof (head));    cnt=0;    memset (instack,0,sizeof (instack));    memset (dfn,0,sizeof (DFN));    top=index=ans=0;    cntin=cntout=0;    memset (In,0,sizeof (in)); Memset (out,0,sizeof (out));}    void Tarjan (int u) {dfn[u]=low[u]=++index;    Instack[u]=1;    Stak[++top]=u;        for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;            if (!dfn[v]) {Tarjan (v); Low[u]=min (Low[v],low[u]);    } else if (Instack[v]) low[u]=min (Low[u],dfn[v]);        } if (Dfn[u]==low[u]) {ans++;            while (1) {int v=stak[top--];            Belong[v]=ans;            instack[v]=0;        if (u==v) break;    }}}int Main () {int n, I, J, A;    scanf ("%d", &n);    Init ();        for (I=1; i<=n; i++) {while (scanf ("%d", &a) &&a) {Add (i,a);    }} for (I=1; i<=n; i++) {if (!dfn[i]) Tarjan (i);    } if (ans==1) printf ("1\n0\n");                else {for (i=1; i<=n; i++) {for (j=head[i]; j!=-1; j=edge[j].next) {                int v=edge[j].v;                    if (Belong[i]!=belong[v]) {in[belong[v]]++;                out[belong[i]]++;            }}} for (I=1; i<=ans; i++) {if (!in[i]) cntin++;      if (!out[i])          cntout++;    } printf ("%d\n%d\n", Cntin,max (Cntin,cntout)); } return 0;}


POJ 1236 Network of schools (strong connected components)

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.