耗费了大量的时间,很努力的去看,但是还是看不懂,水平依旧太低……
program Tarjan;
type
atp=record
x,y,next:longint;
end;
var
i,m,n,x,y,num,sum,DFN:longint;
flag:array [1..10000] of boolean;
low,index,ans,stack:array [1..10000] of longint;
map:array [1..100000] of atp;
procedure work(v:longint);
var
temp,temp1:longint;
begin
inc(DFN);
temp:=index[v];
temp1:=DFN;
low[v]:=DFN;
while temp<>-1 do
begin
if flag[map[temp].y] then
begin
inc(num);
stack[num]:=map[temp].y;
flag[map[temp].y]:=false;
work(map[temp].y);
if low[v]>low[map[temp].y] then low[v]:=low[map[temp].y];
end
else if ans[map[temp].y]=-1 then if low[v]>low[map[temp].y] then low[v]:=low[map[temp].y];
temp:=map[temp].next;
end;
if low[v]=temp1 then
begin
inc(sum);
while stack[num+1]<>v do
begin
ans[stack[num]]:=sum;
dec(num);
end;
end;
end;
begin
assign(input,'tarjan.in'); reset(input);
assign(output,'tarjan.out'); rewrite(output);
readln(m,n);
for i:=1 to n do index[i]:=-1;
for i:=1 to m do
begin
readln(x,y);
map[i].x:=x;
map[i].y:=y;
map[i].next:=index[x];
index[x]:=i;
end;
sum:=0;
for i:=1 to n do
begin
ans[i]:=-1;
flag[i]:=true;
end;
for i:=1 to n do
begin
if ans[i]=-1 then
begin
num:=1;
DFN:=0;
stack[1]:=i;
work(i);
end;
end;
for i:=1 to n do write(ans[i],' ');
close(input); close(output);
end.
218

被折叠的 条评论
为什么被折叠?



