POJ 1236 Network of schools

Source: Internet
Author: User

Main topic:

Given a direction graph, ask:
1) At least a few vertices must be selected in order to get from these vertices
All vertices can be reached.
2) At least how many edges must be added to make the top
Point of departure, all the vertices can be reached
Number of vertices <= 100

A useful theorem:

A point in which all the degrees in the non-circular graph are not 0. Must be from a certain degree of 0 points from the point of no-ring, so from any of the points not 0 to go back must be terminated at a point with a degree of 0

Problem Solving Ideas:

1. Find all the strongly connected components

2. Each strongly connected component is shrunk to a point, forming a directed acyclic graph Dag.

3. What is the number of vertices with a 0 in the Dag above, and the answer to question 1?

To add a few edges on a dag to make the Dag strong, the answer to question 2 is how much

The method of adding edges: To add an edge for each point with a degree of 0, to add an edge for each point with a degree of 0, to assume that there are N degrees 0 points, m out of a 0 point, Max (M,n) is the solution of the second problem (proving difficult, slightly)

But better think, I want to let those into the degree of 0, and the degree of 0, the point becomes not 0, so that we can form a complete unicom, we have a penetration of 0 of the connection a degree of 0, can disappear two pairs, the rest, we each

Just add one more side to the bar.

When we find the strong Unicom components can be, and then re-composition, and finally arrive at the result, there is an answer to special judgment, because there is only one strong unicom component when there is no need for extra edges, so to special judgment.

#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<cmath>#include<stack>#include<cstring>UsingNamespace std;#defineINF 0XFFFFFFF#defineMAXN 105#defineMin (A, b) (A&LT;B?A:B)BOOLINSTACK[MAXN];intLOW[MAXN], DFN[MAXN], STACK[MAXN], top, time, CNT;intMAPS[MAXN][MAXN];intBELONG[MAXN];intN;vector<int>G[MAXN];voidInit () {memset (Low,0,sizeof(low)); memset (DFN,0,sizeof(DFN)); memset (Instack,false,sizeof(Instack)); memset (Maps,0,sizeof(maps)); CNT= time = top =0;  for(intI=0; i<=n; i++) G[i].clear ();}voidTarjan (intu) {Low[u]= Dfn[u] = + +Time ; Stack[top++] =u; Instack[u]=true; intLen =g[u].size (), V;  for(intI=0; i<len; i++) {v=G[u][i]; if( !Low[v])            {Tarjan (v); Low[u]=min (Low[u], low[v]); } elseif (Instack[v]) {Low[u]=min (Low[u], dfn[v]); }    }    if(Low[u] = =Dfn[u]) {         Do{v= stack[--top]; BELONG[V]=CNT; INSTACK[V]=false; } while(U! =v); CNT++; }}voidsolve () {intINDEGREE[MAXN] = {0}; intOUTDEGREE[MAXN] = {0}; intin =0, out =0;  for(intI=1; i<=n; i++)    {        if(!Low[i])    Tarjan (i); }     for(intI=1; i<=n; i++)    {        intLen =g[i].size (), V;  for(intj=0; j<len; J + +) {v=G[i][j]; Maps[belong[i]][belong[v ]=1; }    }     for(intI=0; i<cnt; i++)    {         for(intj=0; j<cnt; J + +)        {            if(i = =j)Continue; if(Maps[i][j]) {Indegree[j]++; Outdegree[i]++; }        }    }     for(intI=0; i<cnt; i++)    {        if(Indegree[i] = =0) in++; if(Outdegree[i] = =0) out++; } printf ("%d\n", in); if(CNT = =1) printf ("0\n"); Elseprintf ("%d\n", Max (in, Out));}intMain () { while(SCANF ("%d", &n)! =EOF)        {Init ();  for(intI=1; i<=n; i++)        {            intA;  while(SCANF ("%d",&a), a) g[i].push_back (a);    } solve (); } Return0;}

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.