Poj1236_a-network of schools _ Strongly connected components:: Tarjan algorithm

Source: Internet
Author: User
Tags in degrees

Time Limit: 1000MS Memory Limit: 10000K

Description

A number of schools is connected to a computer network. Agreements has been developed among those Schools:each School maintains a list of schools to which it distributes Softwa Re (the "Receiving schools"). Note that if B was in the distribution list of school A, then a does not necessarily appear in the list of school B
You is to write a program this computes the minimal number of schools that must receive a copy of the new software in Ord Er for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure is sending the copy of new software to an arbitrary school, this software would r Each of the schools in the network. To achieve this goal we are having to extend the lists of receivers by new members. Compute the minimal number of extensions that has to is made so this whatever school we send the new software to, it'll Reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.

Input

The first line contains a integer n:the number of schools in the network (2 <= N <= 100). The schools is identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school I. Each list is ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write, lines to the standard output. The first line should contain one positive integer:the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

52 4 3 04 5 0001 0

Sample Output

12
Test Instructions

There are N points on the graph, and some have forward edges.
First question: At least a few points to convey information to ensure that the information throughout the map.
Second question: Add at least a few sides to make any selection point, can spread throughout the graph.

Ideas

A naked problem with a strongly connected component.
A message is received at any point within the strongly connected component, and all other internal points must be able to receive the message. Therefore, each strongly connected component can be shrunk to a single point. The first question is the number of strongly connected components that only need to be examined in degrees 0.
For the second question, the graph is connected to a strong connected component, which can also be manipulated in the graph after the contraction point. The practice here is that the number of strong connected components with a degree of 0 and a degree of 0 in the chart is the answer to the second question. In the case of the central Plains, there is only one strong connected component that requires a special sentence.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5 6 using namespacestd;7 8 Const intMAXN = -+Ten;9 Ten intN; One intIN[MAXN], OUT[MAXN]; A  - /***************************tarjan algorithm Template ***************************/ -vector<int>G[MAXN]; the intMARK[MAXN], ROOT[MAXN], STACK[MAXN];//Timestamp , root (the node with the least time stamp in the current component), Stack - BOOLINSTACK[MAXN];//whether to mark in the stack - intSSC[MAXN];//The number of the strongly connected component where each node resides - intIndex, Ssc_n, Top;//time stamp for search, total number of strongly connected components, stack top pointer +  - voidTarjan (intU//u currently searching for points + { AMark[u] = Root[u] = + + Index;//each point found, the timestamp and root initialization atStack[top + +] = u;//Press Stack -Instack[u] =true;//mark in Stack -  -     intv; -  -      for(intI=0; i< g[u].size (); i++)//Search Down in     { -v =G[u][i]; to         if(Mark[v] = =0)//It 's not even a point. +         { -Tarjan (v);//Search down First the             if(Root[u] > Root[v]) Root[u] = Root[v];//Update Root *         } $         Else if(Instack[v] && root[u] > Mark[v]) Root[u] = Mark[v];//The point is still in the stack, try to see if this point can be a rootPanax Notoginseng     } - /*end of search for current point*/ the     if(Mark[u] = = Root[u])//The current point itself when the root +     { ASsc_n + +;//update the number of strongly connected components the  +          Do{//The element in the stack that is behind it in the stack is in the strongly connected component with which it is rooted -v = stack[--Top]; $INSTACK[V] =false; $SSC[V] = Ssc_n;//make the same mark on the point of the same strong connected branch -} while(V! = u);//until it itself -     } the } - Wuyi voidSSC () the { -memset (Mark,0,sizeofMark);//initializing timestamps and in-stack tags Wumemset (Instack,false,sizeofinstack); -Index = Ssc_n = Top =0;//initialize timestamp, strongly connected component number, stack top pointer About  $      for(intI=1; I<= N; i++)//ensure that all points on the diagram are accessed -         if(Mark[i] = =0) Tarjan (i); - } - /***************************tarjan algorithm Template ***************************/ A  + intMain () the { -     //freopen ("In.txt", "R", stdin); $  thescanf"%d", &N); the      for(intI=1; I<= N; i++) the     { the         intx; -          while(SCANF ("%d", &x), x) in g[i].push_back (x); the     } the  About SSC (); the  the     if(Ssc_n = =1)//only one strong connected component is in the case the     { +cout <<"1\n0\n"; -         return 0; the     }Bayi  theMemset (In,0,sizeofin);//to find the degree and the degree of each strong connected component theMemset (out,0,sizeofOut ); -      for(intu=1; U<= N; u++) -     { the          for(intI=0; i< g[u].size (); i++) the         { the             intv =G[u][i]; the             if(Ssc[u]! = Ssc[v])//u,v two points not in the same strong connected branch -Out[ssc[u]] + +, In[ssc[v]] + +; the         } the     } the 94     intS1 =0, S2 =0;//number of points with an entry degree of 0 and a degree of 0 the      for(intI=1; I<= Ssc_n; i++) the     { the         if(In[i] = =0) S1 + +;98         if(Out[i] = =0) S2 + +; About     } - 101cout << S1 << endl << max (S1, S2) <<Endl;102 103     return 0;104}

Poj1236_a-network of schools _ Strongly connected components:: Tarjan algorithm

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.