vijosp1697平面几何——带权并查集

一、一些概念
xor:亦或,相同为0,不同为1
or:或,有一个为1则为1
本题中用0代表平行,1代表垂直
way[i]表示i与i的father[i]是垂直还是平行(值为0或1)
题目中读入的平行垂直关系和way[]表示的都是两个直线之间的平行垂直关系,因此都用xor运算
二、并查集的程序段
1、初始化
for i:=1 to n do
begin
father[i]:=i;
way[i]:=0;
end;
2、找父亲
function getfather(x:longint):longint;
var t:longint;
begin
if father[x]=x then exit(x);
t:=father[x];
father[x]:=getfather(t);//压缩路径
way[x]:=way[t] xor way[x];//在找父亲的过程中一层层合并,最终直接指向最远的父亲;且getfather的过程在getdata之前完成
getfather:=father[x];//每一次都返回最远的父亲
end;

3、得到两个元素之间的关系
function getdata(x,y:longint):longint;
begin
getdata:=way[x] xor way[y];
end;

4、合并两个元素
proedure union(x,y,d:longint);
var a,b:longint;
begin
a:=getfather(x);
b:=getfather(y);
father[a]:=b;
way[a]:=way[x] xor d xor way[y];
end;

三、code

program tt;
var a,b,b1,a1,i,j,n,m,q,d,ans:longint;
    father:array[1..1000]of longint;
    way:array[1..1000]of byte;
    ch,s2:char;
function getfather(x:longint):longint;
var t:longint;
begin
 if father[x]=x then exit(x);
 t:=father[x];
 father[x]:=getfather(t);
 way[x]:=way[t] xor way[x];
 getfather:=father[x];
end;
function getdata(a,b:longint):longint;
begin
 getdata:=way[a] xor way[b];
end;
procedure union(x,y,d:longint);
var a,b:longint;
begin
 a:=getfather(x);
 b:=getfather(y);
 father[a]:=b;
 way[a]:=way[a1] xor d xor way[b1];//这里x,y的值会变!!,要用原始的a1,b1
end;
begin
 readln(n,m,q);
 for i:=1 to n do begin father[i]:=i;way[i]:=0;end;
 for i:=1 to m do
  begin
   readln(ch,a1,ch,s2,ch,ch,b1);
   if s2='p' then d:=0 else d:=1;
   a:=getfather(a1);
   b:=getfather(b1);
   if a<>b then
    union(a,b,d)
   else
    if getdata(a1,b1)<>d then
     begin
      writeln('There must be something wrong...');
      exit;
     end;
  end;
 for i:=1 to n-1 do
  for j:=i+1 to n do
   begin
    a:=getfather(i);
    b:=getfather(j);
    if (a=b)and(getdata(i,j)=0) then ans:=ans+1;
   end;
 writeln(ans);
 for i:=1 to q do
  begin
   readln(ch,a1,ch,ch,b1);
   a:=getfather(a1);
   b:=getfather(b1);
   if a<>b then
    writeln('No idea.')
   else
    if getdata(a1,b1)=0 then writeln('Parallel.') else writeln('Vertical.');
  end;
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值