码迷,mamicode.com
首页 > 其他好文 > 详细

初学并查集-3:浴火银河星际跳跃

时间:2015-01-13 19:25:32      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

题目描述 Description
小 K 又在玩浴火银河了。。。不过这次他的目的不是跑运输赚钱,而
是做任务赚钱。
他想知道关于一个任务的两个星系是否可以连通。
输入描述 Input Description
第一行,三个数,X,N,M
X 表示出现的星系代号的最大值;
N 表示有 N 个星际跳跃门;
M 表示有 M 个任务。
接下来的 N 行描述每个星际跳跃门:每行为两个数字(星系代号),
星际跳跃门连通这两个星系(星际跳跃门是可以双向通行的)
接下来的 M 行表示每个任务需要到达的星系,每个任务需要到达两
个星系。
输出描述 Output Description
共 M 行。
第 i 行表示第 i 个任务是否能完成:即两个星系是否能连通。(能→
Yes;不能→No)
样例输入 Sample Input
5 2 1
3 5
4 5
3 4
样例输出 Sample Output

Yes

var total,n,m:longint;
    father:array[1..10000000]of longint;
    used:array[1..10000000]of boolean;
    i,j,k,x,y:longint;
    yo:boolean;

function find(x:longint):longint;
         begin if father[x]=x
                  then exit(x);
               father[x]:=find(father[x]);
               exit(father[x]);
         end;

procedure union(a,b:longint);
          var i,j,k:longint;
          begin father[find(a)]:=find(father[b]);
          end;


begin yo:=false;
      fillchar(father,sizeof(father),0);
      fillchar(used,sizeof(used),true);
      readln(total,n,m);
      for i:=1 to total do
          father[i]:=i;
      for i:=1 to n do
          begin readln(x,y);
                if find(x)<>find(y)
                   then union(x,y);
          end;
      for i:=1 to m do
          begin readln(x,y);
                if find(x)=find(y)
                    then writeln(‘Yes‘)
                    else writeln(‘No‘);
          end;
end.

初学并查集-3:浴火银河星际跳跃

标签:

原文地址:http://www.cnblogs.com/spiderKK/p/4221915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!