Network of Schools+强联通分量+POJ

本文探讨了一种优化学校间软件分发网络的方法,旨在减少软件分发成本并确保网络内的所有学校都能接收到最新软件。通过分析网络结构,确定最小数量的关键学校作为分发节点,并提出扩展网络连接以实现任意学校作为起点都能覆盖全网的方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10910 Accepted: 4346

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

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2
解决方案:wa了几遍,一直没想明白第二个问,后来看了discuss,才知道一个规律,要添加的最少边数为max(in,out)即出度为0的个数和入度为0的个数两者的最大值,连的时候,要保证每个出度为0的店或每个入度为0的点都连上,这样就能保证整图成一个强联通图。
code:
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
#define MMAX 103
using namespace std;
int low[MMAX],dfn[MMAX],instack[MMAX],fa[MMAX],in[MMAX],out[MMAX],ii[MMAX],oo[MMAX];
int cnt;
vector<int>Map[MMAX];
stack<int>S;
int N,index;
void init()
{
    index=0;
    cnt=0;
    for(int i=0; i<=N; i++)
    {
        Map[i].clear();
        dfn[i]=0;
        low[i]=0;
        in[i]=0;
        out[i]=0;
        fa[i]=i;
        oo[i]=0;
        ii[i]=0;
    }
    memset(instack,0,sizeof(instack));
    while(!S.empty())S.pop();
}
void tarjan(int u)
{

    low[u]=dfn[u]=index++;
    instack[u]=1;
    S.push(u);
    int len=Map[u].size();
    for(int i=0; i<len; i++)
    {
        int v=Map[u][i];
        if(!instack[v])
        {
            tarjan(v);
            low[u]=min(low[v],low[u]);
        }
        else if(instack[v]==1)
        {
            low[u]=min(low[v],low[u]);
        }
    }
    if(low[u]==dfn[u])
    {
        while(!S.empty())
        {
            int temp=S.top();
            S.pop();
            fa[temp]=u;
            instack[temp]=2;
            if(temp==u) break;
        }
        cnt++;
    }
}
int main()
{
    while(~scanf("%d",&N))
    {
        init();
        for(int i=1; i<=N; i++)
        {
            while(1)
            {
                int a;
                scanf("%d",&a);
                if(a==0) break;
                Map[i].push_back(a);
            }
        }
        for(int i=1; i<=N; i++)
        {
            if(!instack[i])
            {
                tarjan(i);
            }
        }
        for(int i=1; i<=N; i++)
        {
            int len=Map[i].size();
            for(int j=0; j<len; j++)
            {
                int v=Map[i][j];
                if(fa[v]!=fa[i])
                {
                    in[fa[v]]++,out[fa[i]]++;
                }
            }
        }
        int _in=0,_out=0;
        for(int i=1; i<=N; i++)
        {
            if(!in[fa[i]])
            {
                ii[fa[i]]=1;
            }
            if(!out[fa[i]])
            {
                oo[fa[i]]=1;
            }
        }
        for(int i=1;i<=N;i++){
            _in+=ii[i];
            _out+=oo[i];
        }
        if(cnt==1)
        cout<<_in<<"\n"<<'0'<<endl;
        else cout<<_in<<"\n"<<max(_in,_out)<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值