[vijos P1112] 小胖的奇偶

时间:2021-02-12 21:05:58

第一次看到这题怎么也不会想到是并查集题目…星期五第一次看到这题,到今天做出来,实在是废了好多功夫。看了很多人的解题都有same和diff数组,我也写了,后来发现不对啊两个数组的话find函数怎么写呢?hash也很久没写了,导致自己测试的时候有两个点没过是因为hash弄错了。

我的程序里f[x]= same[x] (x<=max)

= diff[x-max] (x>max)

总算,最后还是通过了。又是一道想不到思路就写不出的题。据说这题和食物链很像,那么食物链就是我的下一个目标~总感觉并查集是有点抽象的,我还没想到它怎么和树形结构具体联系起来。

BTW,vijos的数据挺弱的…

program vijos_p1112;
const BLOCK=;
var hash,f:array[..] of longint;
n,m,i,a,b,t:longint;
s:string;
function haha(x:longint):longint;
var t:longint;
begin
t:=x mod ;
while (hash[t]<>-) and (hash[t]<>x) do inc(t);
exit(t);
end; function find(x:longint):longint;
begin
if f[x]=x then exit(x) else exit(find(f[x]));
end; procedure union(x,y:longint);
var fx,fy:longint;
begin
fx:=find(x);
fy:=find(y);
if fx<>fy then f[fx]:=fy;
end; begin
readln(n);
readln(m);
if m= then
begin
writeln();
halt;
end;
for i:= to do
begin
f[i]:=i;
hash[i]:=-;
end;
for i:= to m do
begin
readln(s);
t:=pos(' ',s);
val(copy(s,,t-),a);
delete(s,,t);
t:=pos(' ',s);
val(copy(s,,t-),b);
delete(s,,t);
if s='even' then
begin
if find(haha(a-))=find(haha(b+BLOCK)) then
begin
writeln(i-);
halt;
end
else
begin
union(haha(a-),haha(b));
union(haha(a-+BLOCK),haha(b+BLOCK));
end;
end
else
begin
if find(haha(a-))=find(haha(b)) then
begin
writeln(i-);
halt;
end
else
begin
union(haha(a-),haha(b+BLOCK));
union(haha(a-+BLOCK),haha(b));
end;
end;
end;
writeln(m);
end.

小胖的奇偶

测试数据 #0: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 1360 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1364 KiB, score = 10
测试数据 #4: Accepted, time = 15 ms, mem = 1364 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 1360 KiB, score = 10
测试数据 #6: Accepted, time = 15 ms, mem = 1368 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 1364 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 1364 KiB, score = 10
测试数据 #9: Accepted, time = 0 ms, mem = 1364 KiB, score = 10

话说把奇怪的写失败了的东西也扔上来吧0 0

program vijos_p1112;
var same,diff:array[..] of integer;
haha,f:array[..] of longint;
n,m,i,j,a,b,t:longint;
s:string;
function hash(x:longint):longint;
var t:longint;
begin
t:=((x mod )*(x mod )) mod +;
while (haha[t]<>) and (haha[t]<>x) do
inc(t);
haha[t]:=x;
exit(t);
end; {function find(x:longint):longint; same &diff de find ya fen kai
begin
if x=f[x] then exit(x) else exit(find(f[x]));
end; } function sfind(x:longint):longint;
begin
if hash(x)=same[hash(x)] then exit(hash(x)) else exit(sfind(same[hash(x)]));
end; function dfind(x:longint):longint;
begin
if hash(x)=diff[hash(x)] then exit(hash(x)) else exit(dfind(diff[hash(x)]));
end; procedure union(x,y:longint;a,b:word);
var fx,fy:longint;
begin
if a= then fx:=sfind(x) else fx:=dfind(x);
if b= then fy:=sfind(y) else fy:=dfind(y);
if (fx<>fy) then f[fx]:=fy;
end; function pd:boolean;
var buzhidao:integer;
begin
if s='even' then
begin
if sfind(same[hash(a-)])=dfind(diff[hash(b)]) then
begin
writeln(i-);
halt;
end;
end
else
begin
if sfind(same[hash(a-)])=sfind(same[hash(b)]) then
begin
writeln(i-);
halt;
end;
end;
pd:=true;
end; begin
readln(n);
readln(m);
for i:= to m* do
begin
same[i]:=i;
diff[i]:=i+;
end;
for i:= to m do
begin
readln(s);
t:=pos(' ',s);
val(copy(s,,t-),a);
delete(s,,t);
t:=pos(' ',s);
val(copy(s,,t-),b);
delete(s,,t);
if s='even' then
begin
if pd then
begin
union(same[hash(a-)],same[hash(b)],,);
union(diff[hash(a-)],diff[hash(b)],,);
end;
end
else
begin
if pd then
begin
union(same[hash(a-)],diff[hash(b)],,);
union(diff[hash(a-)],same[hash(b)],,);
end;
end;
end;
end.

小胖的奇偶-demo