Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20114   Accepted: 7915

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

Source

分析:参见传送门
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 50010;

int n,m,head[maxn],to[maxn],nextt[maxn],tot = 1,pre[maxn],low[maxn],scc[maxn],cnt,du[maxn],dfs_clock;
int ans,numm,num[maxn],num1,num2,ru[maxn],chu[maxn],ru2[maxn],chu2[maxn],num3;
stack <int> s;

void add(int x,int y)
{
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

void tarjan(int u)
{
    s.push(u);
    pre[u] = low[u] = ++dfs_clock;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (!pre[v])
        {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else
            if (!scc[v])
                low[u] = min(low[u],pre[v]);
    }
    if (pre[u] == low[u])
    {
        cnt++;
        while(1)
        {
            int t = s.top();
            s.pop();
            scc[t] = cnt;
            num[cnt]++;
            if(t == u)
                break;
        }
    }
}

void init()
{
    tot = 1;
    memset(head,0,sizeof(head));
    memset(ru,0,sizeof(ru));
    memset(chu,0,sizeof(chu));
    memset(chu2,0,sizeof(chu2));
    memset(ru2,0,sizeof(ru2));
    memset(pre,0,sizeof(pre));
    memset(scc,0,sizeof(scc));
    memset(low,0,sizeof(low));
    dfs_clock = 0;
    cnt = 0;
    num1 = num2 = num3 = 0;
}

int main()
{
    while (scanf("%d",&n) != EOF)
    {
    init();
    int p;
    for (int i = 1; i <= n; i++)
    {
        while (scanf("%d",&p) && p != 0)
        {
            add(i,p);
            chu2[i]++;
            ru2[p]++;
        }
    }
    for (int i = 1; i <= n; i++)
        if (!pre[i])
            tarjan(i);
    for (int i = 1; i <= n; i++)
        for (int j = head[i];j;j = nextt[j])
    {
        int v = to[j];
        if (scc[i] != scc[v])
        {
            chu[scc[i]]++;
            ru[scc[v]]++;
        }
    }
    for (int i = 1; i <= cnt; i++)
    {
        if (!ru[i])
            num1++;
    }
    for (int i = 1; i <= n; i++)
    {
        if (!ru2[i])
            num2++;
        if (!chu2[i])
            num3++;
    }
    if (cnt != 1)
    printf("%d\n%d\n",num1,max(num2,num3));
    else
        printf("1\n0\n");
    }

    return 0;
}