POJ 1236 Network of schools

Source: Internet
Author: User

Description

Some schools connect to a computer network. The schools have agreed that each school will distribute software (known as "school acceptance") to some other schools. Note If B is in the distribution list of school A, then a does not have to be in the list of B schools.
You have to write a program calculation, according to the Protocol, in order to allow all schools in the network to use the new software, you must accept the minimum number of new software copies of the school (subtask A). Further, we want to make sure that by sending new software to any school, the software is distributed to all schools on the network. In order to complete this task, we may have to expand the receiving school list to join the new members. The calculation requires a minimum of several extensions, so that no matter which school we send new software to, it will reach all the rest of the school (subtask B). An extension is the introduction of a new member in a school's receiving school list.

Input

The first line includes an integer n: the number of schools in the network (2 <= N <= 100). The school is identified with the first N positive integers.
Each row in the next N row represents a Receive school list (distribution list). Line i+1 includes an identifier for school I's receiving school. Each list ends with 0. An empty list is represented by only one 0.

Output

Outputs two lines. The first line should include a positive integer: The solution of subtask A.
The second line should include the solution of sub-task B.

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Hintsource

Usaco
Strong connectivity components, connectivity

A is the number of points on the DAG that are 0 on the Tarjan after the indent . Because if the point X in the degree is 0 , then he cannot obtain the software from other points, therefore must give him a copy software.

B that is, after adding as many edges, theDAG can become a ring. The perceptual understanding is Max( the point with a degree of 0 and a point with a degree of 0 )

1#include <map>2#include <Set>3#include <cmath>4#include <ctime>5#include <queue>6#include <stack>7#include <cstdio>8#include <string>9#include <vector>Ten#include <cstdlib> One#include <cstring> A#include <iostream> -#include <algorithm> - using namespacestd; the #definell Long Long - #defineFile (a) freopen (a ". In", "R", stdin); Freopen (a ". Out", "w", stdout); -  -Inlineintgi () + { -     BOOLb=0;intR=0;CharC=GetChar (); +      while(c<'0'|| C>'9') {if(c=='-') B=!b; C=GetChar ();} A      while(c>='0'&& c<='9') {r=r*Ten+c-'0'; C=GetChar ();} at     if(b)return-R;returnR; - } -  - Const intINF = 1e9+7, N =107, M =10007; - intN,num,f[n],dfn[n],low[n],bl[n],cnt,siz[n],deep,cd[n],rd[n]; - BOOLB[n]; in structData - { to     intnx,fr,to; + }da[m]; -Stack <int>s; the  *InlinevoidAdd (intFrintto ) $ {Panax NotoginsengDA[++NUM].FR=FR, Da[num].to=to, Da[num].nx=f[fr], f[fr]=num; - } the  +InlinevoidTarjan (into) A { thedfn[o]=low[o]=++Deep ; +S.push (o); b[o]=1; -     inti,to; $      for(i=f[o]; i; i=da[i].nx) $         { -to=da[i].to; -             if(!dfn[to]) Tarjan (to), low[o]=min (low[o],low[to]); the             Else if(B[to]) low[o]=min (low[o],low[to]); -         }Wuyi     if(Low[o] = =Dfn[o]) the         { -cnt++; Wu              Do{to=s.top (), S.pop (), b[to]=0, bl[to]=cnt; } while(To! =o); -         } About } $  - intMain () - { - //file ("Schlnet"); An=gi (); +     inti,x,y; the      for(i=1; i<=n; i++) -         { $x=gi (); the              while(x) Add (i,x), x=gi (); the         } the      for(i=1; i<=n; i++)if(!Dfn[i]) Tarjan (i); the     if(CNT = =1) {puts ("1\n0");return 0; } -      for(i=1; i<=num; i++) in         { theX=bl[da[i].fr], y=bl[da[i].to]; the             if(x! = y) cd[x]++, rd[y]++; About         } thex=0, y=0; the      for(i=1; i<=cnt; i++) the         { +             if(!rd[i]) x + +; -             if(!cd[i]) y++; the         }Bayiprintf"%d\n%d\n", X,max (x, y)); the     return 0; the}

POJ 1236 Network of schools

Related Article

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.