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
- SSL1222 矩形(并查集)
- 矩形(并查集)
- SDNU 1016.矩形合并 并查集
- 并查集(集并查)
- Hrbust 1619 只有矩形【逆序思维+枚举+并查集优化】
- 并查集(含关系并查集)
- 【并查集】食物链(关系并查集)
- 【并查集】并查集详解(转)
- 分组并查集(种类并查集)
- 并查集, 畅通工程(简单并查集)
- [数据结构]并查集水体POJ2236(并查集)
- 并查集(disjoint_set)
- 并查集(模板)
- 并查集(亲戚)
- POJ2524(并查集)
- POJ_2236(并查集)
- poj2492(并查集)
- HDU1272(并查集)
- HorizantalScrollView配合ScrollView使用
- 20170301
- __gcd
- #1 TwoSum_leetcode
- war项目分别打成JAR和WAR,并被其他项目依赖
- SSL1222 矩形(并查集)
- 利用jQuery给表格添加分页效果
- 暗号编辑【代码056852】
- Create a mosaic out of several input videos
- linux下使用 du查看某个文件或目录占用磁盘空间的大小方法
- 关于OkHttp缓存post请求的问题
- lx007菲波那切数列(使用递归)
- 跟小博老师一起学JSP ——简介与工作原理
- C#串口通信