poj 1236 Network of Schools

来源:互联网 发布:美国人口普查历年数据 编辑:程序博客网 时间:2024/06/06 03:42
Network of Schools
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 17946
Accepted: 7065

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

这道题和nyoj 120 这道题很像,但是这题多了个输出。
1.首先输出点为入度为0 的个数;
2.在输出我们需要加多少条关系可以让学校之间两两互通。(也就是入度和出度的最大值)
注意:这道题在只有一个点的时候 是要输出
1
0
这一点坑到了。。。。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;#define MAX 105int map[MAX][MAX], DFN[MAX], low[MAX], in[MAX], out[MAX];int flag[MAX], step[MAX];stack<int>S;int res, tot, M, ans, ans1;void Init(){    memset(map, 0, sizeof(map));    memset(DFN, 0, sizeof(DFN));    memset(low, 0, sizeof(low));    memset(in, 0, sizeof(in));    memset(out, 0, sizeof(out));    memset(flag, 0, sizeof(flag));    if(!S.empty())        S.pop();    tot = 0, res = 0;}void tarjan(int v){    DFN[v] = low[v] = ++tot;///初始化两个值,自己为能找到的最先访问的祖先    int u;    flag[v] = 1;    S.push(v);    for(int i = 1; i <= M; i++)    {        if(map[v][i])        {            if(!DFN[i]) ///如果该点没有访问过            {                tarjan(i);                low[v] = min(low[v], low[i]);            }            else if(flag[i])            {                low[v] = min(low[v], low[i]);            }        }    }    if(DFN[v]==low[v])    {        ++res;        do        {            u = S.top();            S.pop();            flag[u] = 0;            step[u] = res;        }        while(v!=u);    }}void solve(){    for(int i = 1; i <= M; i++)    {        for(int j = 1; j <= M; j++)        {            if(step[i]!=step[j]&&map[i][j])                in[step[j]]++, out[step[i]]++;        }    }    int xx = 0, yy = 0;    for(int i = 1; i <= res; i++)    {        if(in[i]==0)        {            xx++;        }        else if(out[i]==0)            yy++;    }    ans = xx;    ans1 = max(xx, yy);    printf("%d\n",ans);    printf("%d\n",ans1);}int main(){    while(~scanf("%d",&M))    {        Init();        for(int i = 1; i <= M; i++)        {            int x;            while(scanf("%d",&x),x)            {                map[i][x] = 1;            }        }        for(int i = 1; i <= M; i++)        {            if(!DFN[i])            {                tarjan(i);            }        }        if(res == 1)        {            printf("1\n0\n");            continue;        }        solve();    }    return 0;}


0 0