POJ 1236 Network of Schools 强连通分量+缩点(tarjan)

http://poj.org/problem?id=1236

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

题目大意:一些学校连接在一个计算机网络上,学校之间存在软件支援协议,每个学校都有它支援的学校名单(单向的)。请计算两个问题的答案:(1)最少需要将一个新软件提供给多少个学校,才能使软件能够通过网络被传送到所有学校。(2)最少需要添加几条新的支援关系,使得一个新软件提供给任何一个学校,其他所有学校就都可以通过网络获得该软件。
思路:把题目给定的关系看做一张有向图, t a r j a n tarjan tarjan计算出强连通分量并进行缩点后,我们得到一张新的有向无环图。对于问题(1),新的图中的零入度点是无法被其他学校支援的,所以这些点必须都提供新软件,非零入度点可以通过其他学校的支援得到软件,因此答案就是零入度点的个数。对于问题(2),其实就是问你给一个有向图最少添加几条边可以使这个图变成强连通图,设 p p p为零出度点的个数, q q q为零入度点的个数,答案就是 m a x ( p , q ) max(p,q) max(p,q)。还有一种特殊情况,就是图本身就是强连通图,那么答案分别是 1 1 1 0 0 0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

const int maxn=105;
const int maxm=1e5+5;

struct edge
{
    int to,nxt;
}Edge[maxm];

int head_sd[maxn];
edge sd[maxm];
int head[maxn],dfn[maxn],low[maxn],Stack[maxn],ins[maxn],id[maxn];
int ind[maxn],outd[maxn];//入度和出度
vector<int> scc[maxn];
int n,tot,num,top,cnt,tol;

void addedge(int x,int y)
{
    Edge[++tot].to=y,Edge[tot].nxt=head[x],head[x]=tot;
}

void addedge_sd(int x,int y)
{
    sd[++tol].to=y,sd[tol].nxt=head_sd[x],head_sd[x]=tol;
}
void tarjan(int x)
{
    dfn[x]=low[x]=++num;
    Stack[++top]=x,ins[x]=1;
    int y;
    for(int i=head[x];i;i=Edge[i].nxt)
    {
        y=Edge[i].to;
        if(!dfn[y])
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(ins[y])
            low[x]=min(low[x],dfn[y]);
    }
    if(dfn[x]==low[x])
    {
        ++cnt;
        do
        {
            y=Stack[top--],ins[y]=0;
            id[y]=cnt,scc[cnt].push_back(y);
        }while(x!=y);
    }
}

int main()
{
    scanf("%d",&n);
    int x,y;
    for(int i=1;i<=n;i++)
    {
        while(~scanf("%d",&y)&&y)
            addedge(i,y);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            tarjan(i);
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j;j=Edge[j].nxt)
        {
            y=Edge[j].to;
            if(id[i]==id[y])
                continue;
            addedge_sd(id[i],id[y]);
        }
    }
    if(cnt==1)
        printf("1\n0\n");
    else
    {
        for(int i=1;i<=cnt;i++)
            for(int j=head_sd[i];j;j=sd[j].nxt)
                outd[i]++,ind[sd[j].to]++;
        int cnt1=0,cnt2=0;
        for(int i=1;i<=cnt;i++)
        {
            if(ind[i]==0)
                ++cnt1;
            if(outd[i]==0)
                ++cnt2;
        }
        printf("%d\n%d\n",cnt1,max(cnt1,cnt2));
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值