题意/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.
题解/solution:
首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点能够找到。可以用Kosaraju或Tarjan。
第二问是添加多少条边可以得到使整个图达到一个强连通分量,结果是MAX(出度为0点的总数,入度为0点的总数)。我觉得是考虑其中的出度为0的点和入度为0的点组成的点集V,将这些点相连,最多这需要max{ans1,ans2}条边,就能使整个图成为强连通分量。
代码/Code:
var
v:array [0..101] of boolean;
map:array [0..101,0..101] of boolean;
order,belong,int,outt:array [0..101] of longint;
n,num,count:longint;
procedure dfs(x:longint);
var
i:longint;
begin
v[x]:=true;
for i:=1 to n do
if (map[x,i]) and (not v[i]) then
dfs(i);
inc(num);
order[num]:=x;
end;
procedure dfst(x:longint);
var
i:longint;
begin
belong[x]:=count;
v[x]:=true;
for i:=1 to n do
if (not v[i]) and (map[i,x]) then
dfst(i);
end;
function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end;
procedure kosaraju;
var
i:longint;
begin
fillchar(v,sizeof(v),0);
num:=0; count:=0;
for i:=1 to n do
if not v[i] then dfs(i);
fillchar(v,sizeof(v),0);
for i:=n downto 1 do
if not v[order[i]] then
begin
inc(count);
dfst(order[i]);
end;
end;
procedure print;
var
i,j,inz,outz:longint;
begin
inz:=0; outz:=0;
fillchar(int,sizeof(int),0);
fillchar(outt,sizeof(outt),0);
for i:=1 to n do
for j:=1 to n do
if (map[i,j]) and (belong[i]<>belong[j]) then
begin
inc(int[belong[j]]);
inc(outt[belong[i]]);
end;
for i:=1 to count do
begin
if int[i]=0 then inc(inz);
if outt[i]=0 then inc(outz);
end;
writeln(inz);
if count=1 then write('0')
else write(max(inz,outz));
end;
procedure main;
var
i,k:longint;
begin
readln(n);
for i:=1 to n do
begin
read(k);
while k<>0 do
begin
map[i,k]:=true;
read(k);
end;
readln;
end;
kosaraju;
print;
end;
begin
main;
end.