Network of Schools(POJ-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

题意:

有n(2<=n<=100)个学校,学校之间有单向网络,一个学校在得到软件之后可以顺着单向网络向其他学校传输

输入给出每个学校可以向其他学校传输的传输列表

1.初始时至少几个学校得到该软件才能使得网络内所有学校都收到该软件

2.至少添加几条边,才能使得向网络中任意一个学校发送该软件,最终能传遍网络中每一个学校

思路:

我们可以先进行缩点求出dag图,然后我们考虑第一个问题,求最少发几套软件可以全覆盖,首先题意已经保证了是联通的。然后我们可以想,如果我们把所有没有入边的点都放上软件,是一定可行的。有入边的一定会通过一些边最终从一定有出边的发放软件的地方获得软件。

然后我们考虑第二个问题。这是一个连通图。如果我们有些点没有入点,有些点没出点。那我们如果想办法将入点和一些出点相连,就能保证最后会成为很多圆相连。这样子答案就是没有入边的点和没有出边的点的最大值。
AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <algorithm>
const int N=10010;
using namespace std;
int n,tot,ans,index;
int dfn[N],low[N],du1[N],du2[N],belong[N];
int stack[N],vis[N];
vector<int>q[N];
void init()
{
    tot=ans=index=0;
    memset(dfn,0,sizeof(dfn));
}
void tarjan(int u)
{
    dfn[u]=low[u]=++tot;
    stack[++index]=u;
    vis[u]=1;
    for(int i=0; i<q[u].size(); i++)
    {
        int v=q[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        ans++;
        int t;
        do
        {
            t=stack[index--];
            vis[t]=0;
            belong[t]=ans;
        }
        while(t!=u);
    }
}
void solve()
{
     for(int i=1; i<=n; i++)
        if(!dfn[i])
            tarjan(i);
}
int main()
{
    scanf("%d",&n);
    init();
    for(int i=1; i<=n; i++)
    {
        int a;
        while(scanf("%d",&a) && a)
            q[i].push_back(a);
    }
    solve();
    if(ans==1)
    {
        printf("1\n0\n");
        return 0;
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=0; j<q[i].size(); j++)
        {
            int v=q[i][j];
            if(belong[i]!=belong[v])
            {
                du1[belong[i]]++;
                du2[belong[v]]++;
            }
        }
    }
    int ans1=0,ans2=0;
    for(int i=1; i<=ans; i++)
    {
        if(du1[i]==0) ans1++;
        if(du2[i]==0) ans2++;
    }
    printf("%d\n%d\n",ans2,max(ans1,ans2));
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值