|
|
вернуться в форумCould anyone tell me why I get TLE #13 using disjoint sets? program ural1160; const maxn=1000; maxm=15000; type edge=record v1,v2:word;l:longint;end; var root:array[1..maxn]of word; e:array[1..maxm]of edge; n,m,i:word; procedure qsort(s,t:word); var p,i,j:word; te:edge; begin if s>=t then exit; p:=s+random(t-s+1); te:=e[p];e[p]:=e[s]; i:=s;j:=t; repeat while (i<j) and (e[j].l>=te.l) do dec(j); if i=j then break;e[i]:=e[j];inc(i); while (i<j) and (e[i].l<=te.l) do inc(i); if i=j then break;e[j]:=e[i];dec(j); until i=j; e[i]:=te; qsort(s,i-1); qsort(i+1,t); end; procedure pathcomp(x:word); var r,t:word; begin r:=x; while root[r]<>r do r:=root[r]; while root[x]<>r do begin t:=root[x];root[x]:=r;x:=t; end; end; begin read(n,m); for i:=1 to m do read(e[i].v1,e[i].v2,e[i].l); qsort(1,m); for i:=1 to n do root[i]:=i; for i:=1 to m do begin pathcomp(e[i].v1);pathcomp(e[i].v2); if root[e[i].v1]<>root[e[i].v2] then begin root[root[e[i].v1]]:=root[e[i].v2]; dec(n); end; if n=1 then break; end; writeln(e[i].l); writeln(i); for n:=1 to i do writeln(e[n].v1,' ',e[n].v2); end. Re: Could anyone tell me why I get TLE #13 using disjoint sets? Try to add {$M 64000000} in front. In fact you got crash(stack overflow) Re: Could anyone tell me why I get TLE #13 using disjoint sets? It's no use to add that. I've tried, but I failed. |
|
|