矩形(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
9
0 3 2 6
4 5 5 7
4 2 6 4
2 0 3 2
5 3 6 4
3 2 5 3
1 4 4 7
0 0 1 4
0 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.