解题报告 sgu 174 walls

题意:给你一些线段,不想交,要求判断给到第几条线段时会出现环。

本人蒟蒻,本人轻易不写数据结构,所以本人就用本人这个很弱很弱的算法 A 了。

离散化+并查集。

先将每一条线段都读入,然后把每一个点都取出来,随机化排序(本人比较习惯于将这些随机打乱顺序之后再排,因为本人太弱,写一些高级的东东总是莫名奇妙的 unACcept。。。。。。),然后捏,把每一个线段的两个端点都用这个序号表示,然后并查集,裸的判断冲突。

本人干了件很经典的事,本人这个程序一共有 n 个线段,也就是最多可能有 n*2 个点,然后本人的点数其实是 tot ,然后本人并查集:

for i:=1 to n do

  f[i]:=i;

o(╯□╰)o。。。。。。orz    无限 WA 了 N 久。

然后,线段一共 20W ,也就是最多可能有 40W 个点,然后本人:

f:array[0..200000]of longint;

无限 RE 了 N 久。。。。。

你并查集跟我有仇吗???????

代码:SueMiller

program ACRush;
var a:array[0..200000,1..6]of longint;
f:array[0..400100]of longint;
l:array[0..400100,1..3]of longint;
i,j,k:longint;
n,tot:longint;
flag:boolean;

procedure qs(ll,rr:longint);
var x,y,i,j:longint;
begin
i:=ll; j:=rr; x:=l[(ll+rr) shr 1,1]; y:=l[(ll+rr) shr 1,2];
repeat
while (l[i,1]<x) or ((l[i,1]=x) and (l[i,2]<y)) do inc(i);
while (x<l[j,1]) or ((x=l[j,1]) and (y<l[j,2])) do dec(j);
if i<=j then
begin
l[0]:=l[i]; l[i]:=l[j]; l[j]:=l[0];
inc(i); dec(j);
end;
until i>j;
if ll<j then qs(ll,j);
if i<rr then qs(i,rr);
end;

function gf(x:longint):longint;
begin
if f[x]=x then
begin
gf:=x;
exit;
end;
f[x]:=gf(f[x]);
gf:=f[x];
end;

procedure union(a,b:longint);
begin
f[gf(a)]:=gf(b);
end;

begin
// assign(input,'1.out');reset(input);
// assign(output,'1.in');rewrite(output);
randomize;
readln(n);
for i:=1 to n do
begin
readln(a[i,1],a[i,2],a[i,3],a[i,4]);
inc(tot);
l[tot,1]:=a[i,1];
l[tot,2]:=a[i,2];
l[tot,3]:=i;
inc(tot);
l[tot,1]:=a[i,3];
l[tot,2]:=a[i,4];
l[tot,3]:=i+n;
end;

k:=0;
for i:=1 to tot do
begin
k:=random(tot+1);
l[0]:=l[k];l[k]:=l[i];l[i]:=l[0];
end;
qs(1,tot);
if l[1,3]<=n then a[l[1,3],5]:=1
else a[l[1,3]-n,6]:=1;

k:=1;
for i:=2 to tot do
if not((l[i,1]=l[i-1,1]) and (l[i,2]=l[i-1,2])) then
begin
inc(k);
if l[i,3]<=n then a[l[i,3],5]:=k else a[l[i,3]-n,6]:=k;
end
else if l[i,3]<=n then a[l[i,3],5]:=k else a[l[i,3]-n,6]:=k;

// for i:=1 to n do
// writeln(a[i,5],' ',a[i,6]);

for i:=1 to tot do
f[i]:=i;
flag:=false;
for i:=1 to n do
begin
if gf(a[i,5])<>gf(a[i,6]) then begin union(a[i,5],a[i,6]);end
else
begin
flag:=true;
writeln(i);
// writeln(a[i,5],' ',a[i,6]);
// writeln(gf(a[i,5]),' ',gf(a[i,6]));
break;
end;
end;

if not flag then writeln(0);
end.

PS:本人一同学,绝恋 LOVE 枫, 3 平衡树,A 了,膜拜ing

原文地址:https://www.cnblogs.com/SueMiller/p/2384873.html