Network of Schools
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 |
[Submit] [Go Back] [Status] [Discuss]
题目大意:给出一个有向图,求最少选取几个点可以遍历到所有点,以及最少加入几条边是图强连通。
第一个问只需要求出缩点后有多少点的入度为0即可。
第二问的话,边数就是max(入度为0的点的个数,出度为0的点的个数),这个不是很好说明白,可以感性的理解一下或者画画图,用zyf的话说想一想就是对的,太神啦。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 20003
using namespace std;
int n,m;
int tot,next[N],point[N],v[N],x[N],y[N],sz,cnt,ed;
int belong[N],st[N],top,ins[N],dfn[N],low[N];
int mark[N],sum,pd[N];
int f[103][103],k;
void add(int x,int y)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
}
void tarjan(int x)
{
dfn[x]=low[x]=++sz;
st[++top]=x; ins[x]=1;
for (int i=point[x];i;i=next[i])
{
int j=v[i];
if (!dfn[j]){
tarjan(j);
low[x]=min(low[x],low[j]);
}
else if (ins[j]) low[x]=min(dfn[j],low[x]);
}
int j;
if (dfn[x]==low[x])
{
cnt++;
do
{
j=st[top--];
ins[j]=0;
belong[j]=cnt;
}while (j!=x);
}
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
int x1;
while (true)
{
scanf("%d",&x1);
if (!x1) break;
ed++; x[ed]=i; y[ed]=x1;
add(x[ed],y[ed]);
}
}
for (int i=1;i<=n;i++)
if (!dfn[i]) tarjan(i);
tot=0;
memset(next,0,sizeof(next));
memset(point,0,sizeof(point));
for (int i=1;i<=ed;i++)
if (belong[x[i]]!=belong[y[i]])
add(belong[x[i]],belong[y[i]]),
mark[belong[y[i]]]++,pd[belong[x[i]]]++;
int ans=0,ans1=0;
for (int i=1;i<=cnt;i++)
{
if (!mark[i]) ans++;
if (!pd[i]) ans1++;
}
printf("%d\n",ans);
if (cnt==1) {
printf("0\n");
return 0;
}
printf("%d\n",max(ans,ans1));
}