/ Vijos / 讨论 / 家族 /

为什么家族都是用并查集?

var i,j,n,m,p,a,b,l:longint;
x:array[0..6001] of longint;
gx:array[0..8000,0..8000] of boolean;
begin
readln(n,m,p);
for i:=1 to n do
for j:=1 to n do
gx[i,j]:=false;
for i:=1 to m do
begin
readln(a,b);
gx[a,b]:=true;
gx[b,a]:=true;
end;
for i:=1 to n do
x[i]:=i;
for l:=1 to 8 do
for i:=1 to n do
for j:=1 to n do
if (gx[i,j]) and (x[j]>x[i]) then
x[j]:=x[i]
else if (gx[i,j]) then x[i]:=x[j];
for i:=1 to p do
begin
readln(a,b);
if x[a]=x[b] then writeln('Yes')
else writeln('No');
end;
end.
这样也都是AC= =虽说8是凑出来的 不过也可以啊。。
测试数据 #0: Accepted, time = 0 ms, mem = 63476 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 63480 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 63476 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 63476 KiB, score = 10
测试数据 #4: Accepted, time = 31 ms, mem = 63476 KiB, score = 10
测试数据 #5: Accepted, time = 156 ms, mem = 63476 KiB, score = 10
测试数据 #6: Accepted, time = 343 ms, mem = 63480 KiB, score = 10
测试数据 #7: Accepted, time = 593 ms, mem = 63476 KiB, score = 10
测试数据 #8: Accepted, time = 968 ms, mem = 63480 KiB, score = 10
测试数据 #9: Accepted, time = 937 ms, mem = 63480 KiB, score = 10
Accepted, time = 3028 ms, mem = 63480 KiB, score = 100

3 条评论

  • @ 2014-11-06 11:19:23

    你可以给数据范围加一个零,再试试比较你的方法和并查集。。

  • @ 2014-11-06 09:28:26

    感觉并查集很方便啊,速度相对又比较快,代码又好写。

  • @ 2014-11-05 22:39:04

    是因为时间太长 易超时吗?

    • @ 2014-11-06 07:30:52

      并查集是一种数据结构 数据结构一般都是为了方便操作或者减少时间复杂度的

  • 1

信息

ID
1034
难度
4
分类
数据结构 | 并查集 点击显示
标签
(无)
递交数
9379
已通过
3848
通过率
41%
被复制
16
上传者