Network of Schools

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
题意:1.要求出至少发分配多少站点,使所有点都能收到,即求入度为0的分量。
2.求要添加多少点,使任意一个点发送物品,其他点都能收到物品,即求Max(入度为0的分量个数,出度为0的分量个数)。
代码:
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a > b ? a : b
#define maxn 101
struct node
{
    
int v, next;
}fn[maxn 
* maxn];
int g[maxn], visit[maxn], dfn[maxn], low[maxn], scc[maxn], stack[maxn], top, num, flag1[maxn], flag2[maxn];
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        g[i] 
= -1, scc[i] = -1, visit[i] = 0, flag1[i] = 0 ,flag2[i] = 0;
    }
}
int tarjan(int u, int times)
{
    low[u] 
= dfn[u] = times;
    visit[u] 
= 1;
    stack[top
++= u;
    
int i, v;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        v 
= fn[i].v;
        
if (!visit[v])
        {
            times 
= tarjan(v, times + 1);
            low[u] 
= Min(low[u], low[v]);
        }
        
else if (scc[v] == -1)
        {
            low[u] 
= Min(low[u], low[v]);
        }
    }
    
if (low[u] == dfn[u])
    {
        num
++;
        
do
        {
            scc[stack[
--top]] = num;
        }
while (stack[top] != u);
    }
    
return times;
}
void circle(int n)
{
    
int times = 0, i;
    top 
= 0, num = 0;
    
for (i = 1; i <= n; i++)
    {
        
if (!visit[i])
        {
            times 
= tarjan(i, times + 1);
        }
    }
}
int main()
{
    
int n, u, v, i, j, ans1, ans2, th;
    
while (scanf("%d"&n) != EOF)
    {
        th 
= 0;
        
set(n);
        
for (u = 1; u <= n; u++)
        {
            
while (scanf("%d"&v), v)
            {
                fn[th].v 
= v, fn[th].next = g[u], g[u] = th++
            }
        }
        circle(n);
        
for (i = 1; i <= n; i++)
        {
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                v 
= fn[j].v;
                
if (scc[i] != scc[v])
                {
                    flag1[scc[v]] 
= 1;//scc[v]有入边 
                    flag2[scc[i]] = 1;//scc[i]有出边 
                }
            }
        }
        
for (i = 1, ans1 = 0, ans2 = 0; i <= num; i++)
        {
            
if (!flag1[i])
            {
                ans1
++;
            }
            
if (!flag2[i])
            {
                ans2
++;
            }
        }
        
if (num == 1)
        {
            printf(
"1\n0\n");
            
continue;
        }
        printf(
"%d\n%d\n", ans1, Max(ans1, ans2));
    }
    
//system("pause");
    return 0;
}