POJ 1236——Network of Schools(强连通)

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

题意:给出n个点的有向图,问1.最少要多少个点才能遍历全部边,2.最少要加多少条边才能使其成为连通图。

思路:强连通缩点,出度个数就是需要的最少遍历图的点个数,max(入度,出度)为最少增加边数,可使成为连通图。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<stack>
using namespace std;
struct node
{
    int to,nextt;
}A[10010];
int tot,head[110],carry,indox;
int DFN[110],LOW[110],book[110],ans[110];
int in[110],out[110];
stack<int>q;
void add(int x,int y)
{
    ++tot;
    A[tot].to=y;
    A[tot].nextt=head[x];
    head[x]=tot;
}
int init()
{
    tot=carry=indox=0;
    memset(head,0,sizeof(head));
    memset(DFN,-1,sizeof(DFN));
    memset(LOW,-1,sizeof(LOW));
    memset(book,0,sizeof(book));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
}
int tarjan(int x)
{
    int tem;
    DFN[x]=LOW[x]=++carry;
    book[x]=1;
    q.push(x);
    for(int i=head[x];i!=0;i=A[i].nextt)
    {
        tem=A[i].to;
        if(DFN[tem]==-1)
        {
            tarjan(tem);
            LOW[x]=min(LOW[x],LOW[tem]);
        }
        else if(book[tem]==1)
        {
            LOW[x]=min(LOW[x],DFN[tem]);
        }
    }
    if(LOW[x]==DFN[x])
    {
        ++indox;
        do
        {
            tem=q.top();
            book[tem]=0;
            ans[tem]=indox;
            q.pop();
        }while(tem!=x);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init();
        int x;
        for(int i=1;i<=n;i++)
        {
            while(~scanf("%d",&x)&&x)
            {
                add(i,x);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(DFN[i]==-1)
                tarjan(i);
        }
        if(indox==1)
        {
            printf("1\n0\n");
            continue;
        }
        int ans1=0,ans2=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j!=0;j=A[j].nextt)
            {
                if(ans[i]!=ans[A[j].to])
                {
                    in[ans[A[j].to]]=1;
                    out[ans[i]]=1;
                }
            }
        }
        for(int i=1;i<=indox;i++)
        {
            if(in[i]==0)
                ans1++;
            if(out[i]==0)
                ans2++;
        }
        printf("%d\n%d\n",ans1,max(ans1,ans2));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/83859381
今日推荐