强连通分量+缩点-poj1236

来源:互联网 发布:专业气象软件 编辑:程序博客网 时间:2024/05/01 02:17
Language:
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9796 Accepted: 3879

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缩点:根据belong数组建新图,以前一直以为缩点还要复杂的操作,原来不用。下面是代码:
#include<iostream>#include<set>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;const int MAX_N=110;int N,num,top,cnt;int map[MAX_N][MAX_N];int dfn[MAX_N],low[MAX_N],pre[MAX_N],belong[MAX_N],stack[MAX_N],instack[MAX_N],cnt_size[MAX_N];int indegree[MAX_N],outdegree[MAX_N];void init(){    memset(map,0,sizeof(map));    memset(instack,0,sizeof(instack));    memset(dfn,0,sizeof(dfn));    memset(cnt_size,0,sizeof(cnt_size));    num=top=cnt=0;}void tarjan(int x){    dfn[x]=low[x]=++num;    stack[++top]=x;    instack[x]=1;    for(int i=1; i<=N; i++)    {        if(!map[x][i])            continue;        if(!dfn[i])        {            tarjan(i);            low[x]=min(low[x],low[i]);        }        else if(instack[i])            low[x]=min(dfn[i],low[x]);    }    if(dfn[x]==low[x])    {        cnt++;        int j;        do        {            j=stack[top--];            instack[j]=0;            belong[j]=cnt;            cnt_size[cnt]++;        }        while(j!=x);    }}void output(){    memset(indegree,0,sizeof(indegree));    memset(outdegree,0,sizeof(outdegree));    int inzero=0,outzero=0;    for(int i=1; i<=N; i++)        for(int j=1; j<=N; j++)        {            if(map[i][j]&&belong[i]!=belong[j])            {                indegree[belong[j]]++;                outdegree[belong[i]]++;            }        }    for(int i=1; i<=cnt; i++)    {        if(!indegree[i])            inzero++;        if(!outdegree[i])            outzero++;    }    if(cnt==1)        cout<<1<<'\n'<<0<<endl;    else        printf("%d\n%d",inzero,max(inzero,outzero));}int main(){    //freopen("in.txt","r",stdin);    int x;    while(cin>>N)    {        init();        for(int i=1; i<=N; i++)        {            while(scanf("%d",&x)!=EOF)            {                if(x==0)                    break;                map[i][x]=1;            }        }        for(int i=1; i<=N; i++)        {            if(!dfn[i])                tarjan(i);        }        output();    }    return 0;}


原创粉丝点击