vj p1034题解
此乃水题!并查集是也!
于是就这样....
代码如下:
1 var n,m,p,i,t,s,a,b:integer;
2 relate:array[0..5000]of integer;
3 function group(a:integer):integer;
4 begin
5 if relate[a]=a then exit(a)
6 else
7 begin
8 relate[a]:=group(relate[a]);
9 exit(relate[a]);
10 end;
11 end;
12 begin
13 readln(n,m,p);
14 for i:=1 to n do relate[i]:=i;
15 for i:=1 to m do
16 begin
17 readln(a,b);
18 t:=group(a);s:=group(b);
19 if t<>s then relate[s]:=t;
20 end;
21 for i:=1 to p do
22 begin
23 readln(a,b);
24 t:=group(a);s:=group(b);
25 if t=s then writeln('Yes')
26 else writeln('No');
27 end;
28 end.
2 relate:array[0..5000]of integer;
3 function group(a:integer):integer;
4 begin
5 if relate[a]=a then exit(a)
6 else
7 begin
8 relate[a]:=group(relate[a]);
9 exit(relate[a]);
10 end;
11 end;
12 begin
13 readln(n,m,p);
14 for i:=1 to n do relate[i]:=i;
15 for i:=1 to m do
16 begin
17 readln(a,b);
18 t:=group(a);s:=group(b);
19 if t<>s then relate[s]:=t;
20 end;
21 for i:=1 to p do
22 begin
23 readln(a,b);
24 t:=group(a);s:=group(b);
25 if t=s then writeln('Yes')
26 else writeln('No');
27 end;
28 end.