记录编号 105473 评测结果 AAAAAAAAAAAA
题目名称 亲戚 最终得分 100
用户昵称 Gravatar筽邝 是否通过 通过
代码语言 Pascal 运行时间 0.146 s
提交时间 2014-06-11 17:54:12 内存使用 0.24 MiB
显示代码纯文本
program cojs259;
var
  fa:array[1..20000]of longint;
  i,n,m,t1,t2,x,y:longint;

function find(x:longint):longint;
var
  t,p,q:longint;
begin
  if fa[x]<0 then exit(x);
  q:=x;
  while fa[q]>0 do
    q:=fa[q];
  p:=x;
  while fa[p]>0 do
  begin
    t:=p; fa[t]:=q; p:=fa[p];
  end;
  exit(q);
end;

procedure union(x,y:longint);
var
  t:longint;
begin
  t:=fa[x]+fa[y];
  if fa[x]>fa[y] then
  begin
    fa[x]:=y;
	fa[y]:=t;
  end else begin
    fa[y]:=x;
	fa[x]:=t;
  end;
end;

begin
assign(input,'relations.in');reset(input);
assign(output,'relations.out');rewrite(output);

  readln(n,m);
  for i:=1 to n do
    fa[i]:=-1;
  for i:=1 to m do
  begin
    readln(x,y);
	t1:=find(x);
	t2:=find(y);
        if t1<>t2 then union(t1,t2);
  end;

  readln(m);
  for i:=1 to m do
  begin
    readln(x,y);
	t1:=find(x);
	t2:=find(y);
	if t1=t2 then writeln('Yes') else writeln('No');
  end;

close(input);close(output);
end.