SSL1222 矩形(并查集)

来源:互联网 发布:众力网络安装电话 编辑:程序博客网 时间:2024/05/29 09:12

矩形(difficult)

Time Limit:20000MS  Memory Limit:65536K
Total Submit:189 Accepted:60
Case Time Limit:2000MS

Description

在一个平面上有n个矩形。每个矩形的边都平行于坐标轴并且都具有值为整数的顶点。我们用如下的方式来定义块。
 每一个矩形都是一个块。
 如果两个不同的矩形有公共线段,那么它们就组成了一个新的块来覆盖它们原来的两个块。
例子:
在图1中的矩形组成了两个不同的块。

写一个程序:
 从文件PRO.IN中读入矩形的个数以及它们的顶点。
 找出这些矩形形成的不同的块的个数。
 将结果写入文件PRO.OUT。

Input

在输入文件PRO.IN的第一行又一个整数n,1 <= n <=7000,表示矩形的个数。接下来的n行描述矩形的顶点,每个矩形用四个数来描述:左下顶点坐标(x,y)与右上顶点坐标(x,y)。每个矩形的坐标都是不超过10000的非负整数。

Output

在文件PRO.OUT的第一行应当仅有一个整数---表示由给定矩形组成的不同的块的个数。

Sample Input

90 3 2 64 5 5 74 2 6 42 0 3 25 3 6 43 2 5 31 4 4 70 0 1 40 0 4 1

Sample Output

2
分析:根据坐标判断两个矩形是否相交,是就把它们连通,最后统计并查集中父节点为0的即可
代码
const  maxn=10000;var  a:array[0..maxn,1..4] of longint;  p:array[0..maxn] of longint;  p1,p2,i,j,n,ans:longint;function check(x,y:longint):boolean;begin  check:=true;  if (a[x,1]>a[y,3]) or (a[x,3]<a[y,1]) then exit(false);  if (a[x,2]>a[y,4]) or (a[x,4]<a[y,2]) then exit(false);  if (a[x,1]=a[y,3]) and (a[x,2]=a[y,4]) then exit(false);  if (a[x,3]=a[y,1]) and (a[x,4]=a[y,2]) then exit(false);  if (a[x,1]=a[y,3]) and (a[x,4]=a[y,2]) then exit(false);  if (a[x,3]=a[y,1]) and (a[x,2]=a[y,4]) then exit(false);end;function find(x:longint):longint;var  y:longint;begin  y:=x;  while p[y]<>0 do    y:=p[y];  exit(y);end;begin  readln(n);  for i:=1 to n do    for j:=1 to 4 do      read(a[i,j]);  for i:=1 to n do    for j:=i+1 to n do      if check(i,j) then        begin          p1:=find(i);          p2:=find(j);          if p1<>p2 then p[p2]:=p1;        end;  for i:=1 to n do    if p[i]=0 then inc(ans);  writeln(ans);end.
0 0
原创粉丝点击