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
题目大意:
给定N个点的关系,求N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
思路:
假设有1个学校得到了软件,那么与它直接相连或是间接相连的学校都可以通过它获得软件,即1个强连通分量需要1套软件
于是我们就找到了强力的强连通分量算法Tarjan(看名字是不是很强力)
得出每一个学校在哪一个强连通分量里。然后算出入度为0的强连通分量的个数,就是第一个答案
统计入度为0和出度为0的强连通分量个数,较大那个就是答案二
注意如果强连通分量的个数是一个,那么第二个的答案是0
源代码/pas:
type
point=record
x,y,next:longint;
end;
var
m,t, maxE:longint;
e:array[0..15000]of point;
ls,low,dfn,s,comp:array[0..15000]of longint;
v:array[0..15000]of boolean;
function min(a,b:longint):longint;
begin
min:=a;
if b<a then
min:=b;
end;
procedure tarjan(x:longint);
var
i,y:longint;
begin
inc(t);
dfn[x]:=t;
low[x]:=t;
inc(s[0]);
s[s[0]]:=x;
v[x]:=true;
i:=ls[x];
while i>0 do
begin
y:=e[i].y;
if not v[y] then
begin
tarjan(y);
low[x]:=min(low[y],low[x]);
end
else
low[x]:=min(low[x],dfn[y]);
i:=e[i].next;
end;
if dfn[x]=low[x] then
begin
inc(comp[0]);
repeat
y:=s[s[0]];
comp[y]:=comp[0];
dec(s[0]);
until x=y;
end;
end;
procedure main;
var
i:longint;
begin
for i:=1 to m do
begin
t:=0;
if not v[i] then
tarjan(i);
end;
end;
procedure print;
var
i,x,y:longint;
indeg,oudeg:array[0..15000]of longint;
begin
x:=0;
y:=0;
fillchar(indeg,sizeof(indeg),0);
fillchar(oudeg,sizeof(oudeg),0);
if comp[0]=1 then
begin
writeln(1);
writeln(0);
exit;
end;
for i:=1 to maxE do
if (comp[e[i].x]<>comp[e[i].y]) then
begin
inc(oudeg[comp[e[i].x]]);
inc(indeg[comp[e[i].y]]);
end;
for i:=1 to comp[0] do
if indeg[i]=0 then inc(x);
for i:=1 to comp[0] do
if oudeg[i]=0 then inc(y);
writeln(x);
writeln(x+y-min(x,y));
end;
procedure init;
var
i,j:longint;
begin
readln(m);
for i:=1 to m do
begin
read(j);
while j<>0 do
begin
inc(maxE);
with e[maxE] do
begin
x:=i;
y:=j;
next:=ls[x];
ls[x]:=maxE;
end;
read(j);
end;
end;
end;
begin
init;
main;
print;
end.