/ Vijos / 讨论 / 问答 /

P1321魔塔 用PASCAL更快AC的方法是什么

我个人很弱,所以只打了边表+深搜,求大神给出PASCAL代码能够很快AC的,或者帮我改一下,时间优化一下!
在VIJOS上面可以过,但是一些网站大数据会卡住。我希望有大神说一下PASCAL更快AC方法!
var n,i,j,m,x,y,z,tot,tot1:longint;
num,next,point,num1,next1,point1:array[0..150001] of longint;
head,head1:array[0..70001] of longint;
p:array[0..70001] of 0..1;
procedure add(u,v,w:longint);
begin
tot:=tot+1;
next[tot]:=head[u];
head[u]:=tot;
point[tot]:=v;
num[tot]:=w;
end;
procedure add1(u,v,w:longint);
begin
tot1:=tot1+1;
next1[tot1]:=head1[u];
head1[u]:=tot1;
point1[tot1]:=v;
num1[tot1]:=w;
end;
procedure DFS(x:longint);
var i:longint;
begin
if p[x]=1 then exit;
i:=head[x];
p[x]:=1;
while i<>-1 do
begin
if (p[point[i]]=0)and(p[num[i]]=1) then DFS(point[i]);
if (p[point[i]]=1)and(p[num[i]]=0) then DFS(num[i]);
i:=next[i];
end;
i:=head1[x];
while i<>-1 do
begin
if (p[point1[i]]=0)and(p[num1[i]]=1) then DFS(point1[i]);
if (p[point1[i]]=1)and(p[num1[i]]=0) then DFS(num1[i]);
i:=next1[i];
end;
end;
begin
readln(n,j,m);
fillchar(head,sizeof(head),255);
fillchar(head1,sizeof(head1),255);
fillchar(p,sizeof(p),0);
for i:=1 to m do
begin
readln(x,y,z);
add(x,y,z); add(y,x,z);
add1(x,z,y); add1(z,x,y);
end;
DFS(j);
for i:=1 to n do
if p[i]=0 then writeln(i,':No')
else writeln(i,':Yes');
end.
测试数据 #0: Accepted, time = 0 ms, mem = 4940 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 4944 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 4940 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 4940 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 4940 KiB, score = 10
测试数据 #5: Accepted, time = 31 ms, mem = 4940 KiB, score = 10
测试数据 #6: Accepted, time = 31 ms, mem = 4940 KiB, score = 10
测试数据 #7: Accepted, time = 156 ms, mem = 5408 KiB, score = 10
测试数据 #8: Accepted, time = 203 ms, mem = 4944 KiB, score = 10
测试数据 #9: Accepted, time = 156 ms, mem = 5408 KiB, score = 10
Accepted, time = 577 ms, mem = 5408 KiB, score = 100

0 条评论

目前还没有评论...