poj1236 Network of Schools

本文介绍了一个基于Tarjan算法解决网络中学校间软件分发问题的方法。具体包括计算最少软件分发点以及使网络成为强连通图所需的最小扩展数量。

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

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21337 Accepted: 8410

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

IOI 1996

题目大意:

       一些学校连入一个电脑网络。那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”)。注意如果 B 在 A 学校的分发列表中,那么 A 不必也在 B 学校的列表中。

       你要写一个程序计算,根据协议,为了让网络中所有的学校都用上新软件,必须接受新软件副本的最少学校数目(子任务A)。更进一步,我们想要确定通过给任意一个学校发送新软件,这个软件就会分发到网络中的所有学校。为了完成这个任务,我们可能必须扩展接收学校列表,使其加入新成员。

       请你计算最少需要增加几个扩展,使得不论我们给哪个学校发送新软件,它都会到达其余所有的学校(子任务B)。一个扩展就是在一个学校的接收学校列表中引入一个新成员。

解析:

       Tarjan强连通分量。

       首先求出强连通分量并缩点,计算出每个缩点后的图的每个节点的入度与出度。

       对于任务A。入度为0的点的个数即为答案。

       对于任务B。B任务的意思是求最少加入几条边,使得整个图是一个强连通分量。强连通分量的主要特征是:每个点的入度和出度都不为0,那么题目就变为了:在入度为0的点和出度为0的点之间最少加多少边,使得没有度数为0的节点。很明显的可以看出,答案就是max(SumIn,SumOut)。


代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <queue>
using namespace std;

const int Max=10105;
int n,m,size=1,sum,ans1,ans2,Index,top;
int first[Max],to[Max],from[Max],exist[Max];
int low[Max],num[Max],father[Max],p[Max];
struct shu{int to,next;};
shu bian[Max];

inline int get_int()
{
   int x=0,f=1;
   char c;
   for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
   if(c=='-') {f=-1;c=getchar();}
   for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
   return x*f;
}

inline void build(int x,int y)
{
   size++;
   bian[size].next=first[x];
   first[x]=size;
   bian[size].to=y;
}

inline void tarjan(int point)
{
   low[point]=num[point]=++Index;
   exist[point]=1;
   p[++top]=point;
   for(int u=first[point];u;u=bian[u].next)
   {
   	 if(!num[bian[u].to])
   	 {
   	   tarjan(bian[u].to);
   	   low[point]=min(low[point],low[bian[u].to]);
   	 }
   	 else if(exist[bian[u].to]) low[point]=min(low[point],num[bian[u].to]); 
   }

   if(low[point]==num[point])
   {
   	 sum++;
   	 while(1)
   	 {
   	   int x=p[top--];
   	   exist[x]=0;
   	   father[x]=sum;
   	   if(x==point) break;
   	 }
   }
}

int main()
{
   //freopen("lx.in","r",stdin);
   //freopen("lx.out","w",stdout);

   scanf("%d",&n);
   for(int i=1;i<=n;i++) father[i]=i;
   for(int i=1;i<=n;i++)
   {
   	 int x;
   	 while(scanf("%d",&x))
   	 {
   	   if(x==0) break;
   	   build(i,x);
   	 }
   }

   for(int i=1;i<=n;i++) if(!num[i]) tarjan(i);

   for(int i=1;i<=n;i++)
     for(int u=first[i];u;u=bian[u].next)
       if(father[i]!=father[bian[u].to])
	   {
	     to[father[bian[u].to]]++;
	     from[father[i]]++;
	   }

   for(int i=1;i<=sum;i++)
   {
     if(to[i]==0) ans1++;
     if(from[i]==0) ans2++;
   }

   cout<<ans1<<"\n";
   if(sum==1) cout<<"0\n";
   else cout<<max(ans1,ans2)<<"\n";
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值