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


题意:本题有2个问题

           第一个是要算最少要给多少个点软件,才能使所有点都可以收到副本

           第二个是要算最少加多少条边,使得图变成强连通

做法:(n久不刷的强连通分量全都就饭吃了QAQ)

第一问就是求有多少个团入度=0

第二问是问max(入度=0的团数,出度=0的团数)

注意原图是强连通图的时候要特判、

#include <iostream>
#include<cstdio>
#include<stack>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 5005
vector<int>G[maxn];
int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;
stack<int>S;
int in0[maxn],out0[maxn];
int num[5006];
void dfs(int u)
{
    pre[u]=lowlink[u]=++dfs_clock;
    S.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!pre[v])
        {
            dfs(v);
            lowlink[u]=min(lowlink[u],lowlink[v]);
        }
        else if(!sccno[v])
            lowlink[u]=min(lowlink[u],pre[v]);
    }
    if(lowlink[u]==pre[u])
    {
        scc_cnt++;
       // printf("序号是%d  ,里面有",scc_cnt);
        for(;;)
        {
            int x=S.top();S.pop();
            sccno[x]=scc_cnt;
            //printf("%d   ",x);
            if(x==u) break;
        }
    }
}
void find_scc(int n)
{
    dfs_clock=scc_cnt=0;
    memset(sccno,0,sizeof(sccno));
    memset(pre,0,sizeof(pre));
    for(int i=0;i<n;i++) if(!pre[i]) dfs(i);
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)G[i].clear();
    for(int i=0;i<n;i++)
    {
        int x;
        while(scanf("%d",&x))
        {
            if(x==0)break;
            x--;
            G[i].push_back(x);
        }
    }
    find_scc(n);
    memset(in0,0,sizeof(in0));
    memset(out0,0,sizeof(out0));
    int a=0,b=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<G[i].size();j++)
        {
            if(sccno[G[i][j]]==sccno[i])continue;
            in0[sccno[G[i][j]]]=1;
            out0[sccno[i]]=1;
        }
    }
    for(int i=1;i<=scc_cnt;i++)
    {
        if(in0[i]==0)a++;
        if(out0[i]==0)b++;
    }
    if(scc_cnt!=1)
    printf("%d\n%d\n",a,max(a,b));
    else printf("1\n0\n");
    return 0;
}