POJ 1236 Network of schools

Source: Internet
Author: User
Tags in degrees

Before the Kosaraju_algorithm understand the wrong, now thoroughly understand.

Kosaraju_algorithm:

• step1: Depth- first traversal of the original G, recording the departure time of each node. Formed a forest (a lot of trees). • Step2: Select the vertices that have the latest departure time, traverse the inverse of the graph GT, and Delete the vertices that can traverse to form a strong connected component. • Step3: If there are vertices not removed, continue to Step2, otherwise the algorithm ends. for this topic, the answer to the first question is the number of nodes after the indentation is 0, the second question is Max (the number of nodes in the 0, the number of nodes entering and leaving 0)If there is only one strong connected component, then output 1 0 is possible.
#include <cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>using namespacestd;Const intmaxn= the;intdfn[maxn],u[100000],v[100000],BELONG[MAXN];intIN[MAXN],OUT[MAXN],FLAG[MAXN];structpoint{intID,DFN;} Point[maxn];vector<int>G[maxn];vector<int>FG[MAXN];intN,tot;intSum,block,inch, out;voidinit () { for(intI=0; i<maxn; i++) g[i].clear ();  for(intI=0; i<maxn; i++) fg[i].clear (); memset (DFN,0,sizeofDFN); Memset (In,0,sizeofIn ); memset (out,0,sizeofOut ); memset (Flag,0,sizeofflag); Tot=0;//time Stampsum=0;//the number of edgesblock=0;//number of strongly connected components    inch=0;//number of points with a count of 0 in degrees     out=0;//number of points with a statistical degree of 0}BOOLcmpConstPoint&a,Constpoint&b) {    returnA.dfn>B.DFN;}voidDfs (intNow ) {Flag[now]=1;  for(intI=0; I<g[now].size (); i++)        if(!Flag[g[now][i]])    Dfs (G[now][i]); Tot++; Dfn[now]=Tot;}voidDfsintNow ) {Belong[now]=Block;  for(intI=0; I<fg[now].size (); i++)        if(!Belong[fg[now][i]]) DFS (fg[now][i]);}intMain () { while(~SCANF ("%d",&N)) {init ();  for(intI=1; i<=n; i++)        {             while(1)            {                intv; scanf ("%d",&v); if(!V) Break; Sum++; U[sum]=i; V[sum]=v;                G[i].push_back (v);            Fg[v].push_back (i); }        }         for(inti =1; I <= N; i++)if(!Flag[i])        Dfs (i);  for(intI=0; i<n; i++) Point[i].id=i+1, point[i].dfn=dfn[i+1]; Sort (Point,point+n,cmp);  for(intI=0; i<n; i++)            if(!Belong[point[i].id]) Block++, DFS (point[i].id);  for(intI=1; i<=sum; i++)            if(belong[u[i]]!=Belong[v[i]]) Out[belong[u[i] ]++,in[belong[v[i]]]++;  for(intI=1; i<=block; i++)        {            if(! In[i])inch++; if(! Out[i]) out++; }        if(block==1) printf ("1\n0\n"); Elseprintf"%d\n%d\n",inch, Max (inch, out)); }    return 0;}

POJ 1236 Network of schools

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.