POJ1236 强连通分支(strongly connected component _kosaraju algorithm)

来源:互联网 发布:新天龙八部辅助软件 编辑:程序博客网 时间:2024/06/13 11:08
题目的意思是:总学校分发软件,每个学校得过软件后,可以通过单向网络分发软件,求(1)最少的软件分发数;(2)添加最少的线路,使发放到任意的学校就可以让所有学校收到。
思路:
(1)有kosaraju algorithm求出各连通分支,然后通过深搜转置的图计算出深度优先树;
(2)在深度优先树中求出出度为0的结点的个数即为第一问的解。因为所有入度不为0的结点可以由入度为0的点出发到达。
(3)求深度优先树的的出度为0的结点与入度为0的点的最大值。
        1、弱连通图没有出度为0或入度为0的点必为强连通图
       
       
Network of Schools
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 4373Accepted: 1710

Description

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will 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 an integer N: the number of schools in the network (2 <= N <= 100). The schools are 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 ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write two 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

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

Sample Output

1
2