poj 1236 Network of Schools

本文介绍了一种针对学校网络的软件分发算法,旨在找到最少的学校节点以确保软件能够覆盖整个网络,并讨论了如何通过增加接收者列表来进一步优化分发流程。

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

Network of Schools

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16697 Accepted: 6600

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

Source


【分析】
tarjan强连通分量缩点的一道题
答案一:缩点后入度为0的点的个数
答案二:max( num(in==0),num(out==0) )
注意如果缩点后只有一个点,答案二为0,被坑了一次


【代码】

#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=105;
vector <int> f[mxn];
stack <int> s;
int n,m,tim,cnt,num;
int dfn[mxn],low[mxn],be[mxn],ru[mxn],chu[mxn],from[mxn*mxn],to[mxn*mxn];
bool in[mxn];
inline void tarjan(int u)
{
    int i,j,v;
    dfn[u]=low[u]=++tim;
    s.push(u),in[u]=1;
    for(i=0;i<f[u].size();i++)
    {
        v=f[u][i];
        if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
        else if(in[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        num++;
        do
        {
            v=s.top();
            s.pop();
            in[v]=0;
            be[v]=num;
        }while(u!=v);
    }
}
int main()
{
    int i,j,u,v,ans=0,sum=0;
    scanf("%d",&n);
    fo(u,1,n)
      while(scanf("%d",&v) && v)
      {
          f[u].push_back(v);
          from[++cnt]=u,to[cnt]=v;
      }
    fo(i,1,n) if(!dfn[i]) tarjan(i);
    fo(i,1,cnt)
    {
        u=from[i],v=to[i];
        if(be[u]!=be[v]) chu[be[u]]++,ru[be[v]]++;
    }
    fo(i,1,num) if(ru[i]==0) ans++;
    printf("%d\n",ans);
    fo(i,1,num) if(chu[i]==0) sum++;
    if(num==1) printf("0\n");
    else printf("%d\n",max(ans,sum));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值