Network of Schools 缩点+强联通

Network of Schools
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 10095Accepted: 4004

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   
 
这个题目真是很变态啊,但是不能不承认这确实是个好题目,这个题目是说有一种软件在学校之间传播,问能至少
有几个软件可以让所有的学校都具有这一个软件,还有就是至少加几个边,让大家只用一个软件就行了,每个点之间
都是联通的,这个文章第一句话已经说明了,第一问是求出来所有强联通分量中出度为0的数,另一个是求其出度和
入度的最大值,自己理解吧,套上Tarjan就行了,belong数组是用来表示每个点属于哪个连通分量的。
 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define M 120
int low[M],dfn[M],map[M][M],in[M],out[M];
int stack[M];
int vis[M];
int belong[M];
int n,cnt,top,num;
void init()
{
    cnt=top=num=0;
    memset(vis,0,sizeof(vis));
    memset(map,0,sizeof(map));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(out,0,sizeof(out));
    memset(in,0,sizeof(in));
}
int min(int a, int b)
{
    return a < b ? a : b;
}

int max(int a, int b)
{
    return a > b ? a : b;
}
void Tarjan(int x)
{
    int i,j,k;
    dfn[x]=low[x]=++cnt;
    stack[top++]=x;
    vis[x]=1;
    for(i=1; i<=n; i++)
    {
        if(!map[x][i])
            continue;
        if(!dfn[i])
        {
            Tarjan(i);
            low[x]=min(low[x],low[i]);
        }
        else if(vis[i])
        {
            low[x]=min(low[x],dfn[i]);

        }
    }
    if(dfn[x]==low[x])
    {
        num++;
        int tmp;
        while(1)
        {
            tmp=stack[--top];
            belong[tmp]=num;
            vis[tmp]=0;
            if(tmp==x)break;
        }

    }
}

int main()
{
    int i,j,k;
    while(scanf("%d",&n)!=EOF)
    {


        init();
        for(i=1; i<=n; i++)
        {
            while(scanf("%d",&k)&&k)
            {
                map[i][k]=1;

            }
        }
        for(i=1; i<=n; i++)
        {
            if(!dfn[i])
            {
                Tarjan(i);
            }
        }
        //for(i=1;i<=n;i++)
        //printf("%d\n",belong[i]);
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(map[i][j]&&belong[i]!=belong[j])
                {

                    in[belong[j]]++;
                    out[belong[i]]++;
                    // printf("fdshuifs");

                }
            }
        }

        int c1=0,c2=0;
        for(i=1; i<=num; i++)
        {
            if(!in[i])c1++;
            if(!out[i])c2++;

        }

        //printf("num%d%d%d\n",num,c1,c2);
        if(num==1)
            printf("1\n0\n");
        else
            printf("%d\n%d\n",c1,max(c1,c2));





    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值