tyvj P1111 - 舞会
P1111 - 舞会From Admin Normal (OI) 总时限:10s 内存限制:128MB |
|||||||
---|---|---|---|---|---|---|---|
|
Delphi语言: 高亮代码由发芽网提供
program tyvj1111;
var a:array[1..200,0..200] of integer;
mark:array[1..200] of boolean;
n,i,k,ans:integer;
procedure dfs(k:integer);
var i:integer;
begin
mark[k]:=true;
for i:=1 to a[k,0] do
if not mark[a[k,i]] then begin
mark[a[k,i]]:=true;
dfs(a[k,i]);
end;
end;
begin
readln(n);
fillchar(mark,sizeof(mark),false);
for i:=1 to n do begin
read(k);
while k<> 0 do begin
inc(a[i,0]);
a[i,a[i,0]]:=k;
read(k);
end;
end;
ans:=0;
for i:=1 to n do
if not mark[i] then begin
inc(ans);dfs(i);
end;
writeln(ans);
end.
Delphi语言: 高亮代码由发芽网提供
program tyvj1111s2;//tarjan求强连通分量做法
var q,dfn,low:array[1..200] of integer;
v:array[1..200] of boolean;
b:array[1..200,0..400] of integer;
c:array[1..200] of integer;
num,k,top,i,n,t,y:longint;
function min(a,b:longint):longint;
begin
if a>b then exit(b)
else exit(a);
end;
procedure tarjan(k:longint);
var i,now:longint;
begin
inc(num);low[k]:=num;dfn[k]:=num;
inc(top);q[top]:=k;v[k]:=true;
for i:=1 to b[k,0] do begin
now:=b[k,i];
if not v[now] then begin
tarjan(now);
low[k]:=min(low[k],low[now]);
end else
low[k]:=min(low[k],dfn[now]);
end;
if low[k]=dfn[k] then begin
inc(t);y:=-1;
while k<>y do begin
y:=q[top];dec(top);
end;
end;
end;
begin
fillchar(v,sizeof(v),false);
readln(n);
for i:=1 to n do begin
read(k);
while k<>0 do begin
inc(b[i,0]);b[i,b[i,0]]:=k;
read(k);
end;
end;
t:=0;num:=0;
for i:=1 to n do if not v[i] then
tarjan(i);
writeln(t);
end.