Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18798 | Accepted: 7400 |
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.
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
——————————————————————————————————
题目的意思是给出一张有向图,文件可沿边发送,问所有点收到文件最少要多少个文件?如果只有一个文件问最少加多少条边才可以让每个点都收到
思路:强联通缩点,找出入度为0的点,加的边的量是max(入度为0的点,出度为0的点)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
#define mem(a,b) memset(a,b,sizeof a)
const int INF = 0x3f3f3f3f;
#define MAXN 100100
#define MAXM 1000100
struct node
{
int u,v,next;
} edge[MAXM];
int dfn[MAXN],low[MAXN],s[MAXN],Stack[MAXN],in[MAXN],block[MAXN],a[MAXN],b[MAXN];
int cnt,tot,index,bnum;
void init()
{
mem(s,-1);
mem(a,0);
mem(b,0);
mem(dfn,0);
mem(low,0);
mem(Stack,0);
mem(in,0);
mem(block,0);
cnt=tot=index=bnum=0;
}
int add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=s[u];
s[u]=cnt++;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tot;
Stack[++index]=x;
in[x]=1;
for(int i=s[x]; ~i; i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
}
else if(in[v])
{
low[x]=min(low[x],low[v]);
}
}
if(dfn[x]==low[x])
{
bnum++;
do
{
// printf("%d ",Stack[index]);
block[Stack[index]]=bnum;
in[Stack[index--]]=0;
}
while(Stack[index+1]!=x);
// printf("\n");
}
}
int main()
{
int n,x;
while(~scanf("%d",&n))
{
init();
for(int i=1; i<=n; i++)
{
while(scanf("%d",&x)&&x)
{
add(i,x);
}
}
for(int i=1; i<=n; i++)
if(!dfn[i]) tarjan(i);
for(int i=0; i<cnt; i++)
{
int x=block[edge[i].u],y=block[edge[i].v];
if(x!=y) a[x]++,b[y]++;
}
int ans1=0,ans2=0;
for(int i=1; i<=bnum; i++)
{
if(!a[i]) ans1++;
if(!b[i])ans2++;
}
if(bnum==1)
printf("1\n0\n");
else
printf("%d\n%d\n",ans2,max(ans1,ans2));
}
return 0;
}