POJ1236【tarjan】

来源:互联网 发布:乐乐体育淘宝 编辑:程序博客网 时间:2024/06/06 01:00
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7348 Accepted: 2894

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缩点。然后求所有点(缩点后的)的入度为0的点的个数和出度为0的点的个数。那么问题一的答案是入度为0的点的个数。问题二是max(入度为0的点的个数,出度为0的点的个数)。

#include<cstdio>#include<cstring>#include<stack>using namespace std;#define MAX_NODE 101bool side[MAX_NODE][MAX_NODE];stack<int>q;int low[MAX_NODE];int dfn[MAX_NODE];int belong[MAX_NODE];int n;int sum;int time;void dfs(int u){    dfn[u]=low[u]=time++;    q.push(u);    for(int i=1;i<=n;i++)if(side[u][i]){        if(!dfn[i])dfs(i);        if(dfn[i]!=-1)low[u]=min(low[u],low[i]);    }    if(dfn[u]==low[u]){        int v;        do{            v=q.top();q.pop();            dfn[v]=-1;            belong[v]=sum;        }while(v!=u);        sum++;    }}void tarjan(){    sum=0;time=1;    while(!q.empty())q.pop();    memset(belong,-1,sizeof(belong));    memset(dfn,0,sizeof(dfn));    for(int i=1;i<=n;i++){        if(!dfn[i])dfs(i);    }}bool in[MAX_NODE];bool out[MAX_NODE];void solve(int &a,int &b){    memset(in,0,sizeof(in));    memset(out,0,sizeof(out));    for(int i=1;i<=n;i++)    for(int j=1;j<=n;j++)    if(side[i][j]){        if(belong[i]!=belong[j]){            out[belong[i]]=true;            in[belong[j]]=true;        }    }    int sum_in=0;int sum_out=0;    for(int i=0;i<sum;i++){        if(!in[i])sum_in++;        if(!out[i])sum_out++;    }    a=sum_in;    b=max(sum_in,sum_out);    if(sum==1){        b=0;        a=1;    }}int main(){    memset(side,0,sizeof(side));    scanf("%d",&n);    for(int from=1;from<=n;from++){        int to;        while(scanf("%d",&to)&&to)side[from][to]=true;    }    tarjan();    int a,b;    solve(a,b);    printf("%d\n%d\n",a,b);}