POJ 1236 Network of Schools

来源:互联网 发布:apache ab源码下载 编辑:程序博客网 时间:2024/06/10 09:47
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5731 Accepted: 2275

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

52 4 3 04 5 0001 0

Sample Output

12

Source

IOI 1996


//Tarjan算法

题意:有一些网络学校,他们各自有一份网络维护清单, 上面写着维护学校的编号

问题:1.至少需要copy多少份软件给那些学校,使得每个学校都可以得到软件

            2.至少需要增加多少个学校的清单项目,才能使用一个软件 可以让每一个学校都得到

分析:应用Tarjan,将原图缩为无环有向图,对于问题1,其实就是求无环有向图的所有点中入度为0的点的个数,记为ans1

            对于问题2,要求所有点中出度为0的点的个数,记为ans2,然后ans1和ans2大的就是答案

注意:当原图是一个强连通分支时,那么就ans1,此时原图将缩成一个点,那么可以看出答案就是1和0

#include <iostream>
#include <vector>
#include <string>
using namespace std;

#define MAXV 102
#define min(x, y) (x)<(y)? (x):(y)
int stack[MAXV], instack[MAXV], top;
int dfn[MAXV], low[MAXV], cnt;
int num[MAXV], scc;
vector <int> edge[MAXV];
vector <int> comp[MAXV];
int g[MAXV][MAXV];
void Tarjan(int i)
{
    int j;
    dfn[i] = low[i] = cnt++;
    stack[++top] = i;
    instack[i] = 1;
    for(int k = 0; k < edge[i].size(); k++)
    {
         j = edge[i][k];
        if(dfn[j] == -1)
        {
            Tarjan(j);
            low[i] = min(low[i], low[j]);
        }
        else if(instack[j])
        {
            low[i] = min(low[i], dfn[j]);
        }
    }
    if(dfn[i] == low[i])
    {
        scc++;
        do
        {
            j = stack[top--];
            instack[j] = 0;
            comp[scc].push_back(j);
            num[j] = scc;
        }while(i != j);
    }
}

void solve(int N)
{
    int i, j;
    memset(instack, 0, sizeof(instack));
    memset(dfn, -1, sizeof(dfn));
    top = scc = 0; cnt = 1;
    for(i = 1; i <= N; i++)
    {
        if(dfn[i] == -1)
            Tarjan(i);
    }
}    

int main()
{
    int i, j, k, x, n;
    while(scanf("%d", &n) != EOF)
    {
        for(i = 0; i <= n; i++)
        {
            edge[i].clear();
            comp[i].clear();
        }
        for(i = 1; i <= n; i++)
        {
            scanf("%d", &x);
            while(x)
            {
                edge[i].push_back(x);
                scanf("%d", &x);
            }
        }
        solve(n);
        if(scc == 1)
        {
            printf("1\n");
            printf("0\n");
            continue;
        }
        memset(g, 0, sizeof(g));
    //    for(i = 1; i <= 5; i++)
    //        cout<<'*'<<num[i]<<endl;
        for(i = 1; i <= n; i++)
        {
            for(j = 0; j < edge[i].size(); j++)
            {
                int y = edge[i][j];
                if(num[i] != num[y])
                {
                    g[num[i]][num[y]] = 1;
                }
            }
        }
        int ans1 = 0, ans2 = 0;
        for(i = 1; i <= scc; i++)
        {
            int flag = 1;
            for(j = 1; j <= scc; j++)
            {
                if(g[j][i]) {
                    flag = 0;
                    break;
                }
            }
            if(flag) ans1++;
            flag = 1;
            for(j = 1; j <= scc; j++)
            {
                if(g[i][j])
                {
                    flag = 0;
                    break;
                }
            }
            if(flag) ans2++;
        }
        printf("%d\n", ans1);
        printf("%d\n", ans1 > ans2? ans1:ans2);
    }
    return 0;
}


原创粉丝点击