【POJ 1236】Network of Schools(强连通分量_tarjan)

问题描述
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.
输入
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.
输出
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.
样例输入
5
2 4 3 0
4 5 0
0
0
1 0
样例输出
1
2
算法讨论
题目大意:N(2<’N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
解法:
1:先跑一边tarjian算法。那么我们可以得出每一个学校在哪一个强连通分量里。然后算出入度为0 的强连通分量的个数。就是第一个答案。
2:读为0的强连通分量的那些强连通分量连起来。所以算出出度为0和入度为0的最大值,就是第二个的答案。
3:注意如果强连通分量的个数是一个,那么第二个的答案是0.

#include<cstdio>
#include<cstring>
using namespace std;
struct edge
{
    int f,t,n;
}g[10005];
int ls[10005],dfn[10005],low[10005],stack[10005],belong[10005];
int cd[105],rd[105];
bool instack[10005];
int n=0,tot=0,bcnt=0,dindex=0;

int trajan(int i)
{
    int j=0,t=0;
    dindex++;
    dfn[i]=dindex;
    low[i]=dindex;
    t=ls[i];
    tot++;
    stack[tot]=i;
    instack[i]=true;
    while (t>0)
    {
        j=g[t].t;
        if (dfn[j]==0)
        {
            trajan(j);
            if (low[i]>low[j])
                low[i]=low[j];
        }
        else
            if ((instack[j])&&(dfn[j]<low[i]))
                low[i]=dfn[j];
        t=g[t].n;
    }
    if (dfn[i]==low[i])
    {
        bcnt++;
        do
        {
            j=stack[tot];
            instack[j]=false;
            tot--;
            belong[j]=bcnt;
        }
        while (i!=j);
    }
    return 0;
}

int main()
{
    scanf("%d",&n);
    int x=0,e=0;
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        while (x!=0)
        {
            e++;
            g[e].f=i;
            g[e].t=x;
            g[e].n=ls[i];
            ls[i]=e;
            scanf("%d",&x);
        }
    }
    for (int i=1;i<=n;i++)
        if (dfn[i]==0)
            trajan(i);
    for (int i=1;i<=e;i++)
        if (belong[g[i].f]!=belong[g[i].t])
        {
            cd[belong[g[i].f]]++;
            rd[belong[g[i].t]]++;
        }
    int ans=0,s1=0,s2=0;
    for (int i=1;i<=bcnt;i++)
    {
        if (rd[i]==0)
        {
            ans++;
            s1++;
        }
        if (cd[i]==0)
            s2++;
    }
    if (bcnt==1)
    {
        printf("1\n0\n");
        return 0;
    }
    printf("%d\n",ans);
    if (bcnt==1)
    {
        s1=0;
        s2=0;
    }
    if (s1>s2)
        printf("%d\n",s1);
    else
        printf("%d\n",s2);
    return 0;
}

这里写图片描述
Pixiv ID:59110247

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值