Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21337 | Accepted: 8410 |
Description
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
Output
Sample Input
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
题目大意:
一些学校连入一个电脑网络。那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”)。注意如果 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;
}