poj 1127:Jack Straws(判断两线段相交 + 并查集)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2911 | Accepted: 1322 |
Description
Input
When n=0,the input is terminated.
There will be no illegal input and there are no zero-length straws.
Output
Sample Input
- 7
- 1 6 3 3
- 4 6 4 9
- 4 5 6 7
- 1 4 3 5
- 3 5 5 5
- 5 2 6 3
- 5 4 7 2
- 1 4
- 1 6
- 3 3
- 6 7
- 2 3
- 1 3
- 0 0
- 2
- 0 2 0 0
- 0 0 0 1
- 1 1
- 2 2
- 1 2
- 0 0
- 0
Sample Output
- CONNECTED
- NOT CONNECTED
- CONNECTED
- CONNECTED
- NOT CONNECTED
- CONNECTED
- CONNECTED
- CONNECTED
- CONNECTED
Source
- int UFS_NUM; //并查集中元素总数
- typedef struct node{
- int data; //节点对应的编号
- int rank; //节点对应秩
- int parent; //节点对应的双亲下标
- }UFSTree; //并查集树的节点类型
- void MAKE_SET(UFSTree t[]) //初始化并查集树
- {
- int i;
- for(i=;i<=UFS_NUM;i++){
- t[i].data = i; //数据为该点编号
- t[i].rank = ; //秩初始化为0
- t[i].parent = i; //双亲初始化为指向自己
- }
- }
- int FIND_SET(UFSTree t[],int x) //在x所在的子树中查找集合编号
- {
- if(t[x].parent == x) //双亲是自己
- return x; //双亲是自己,返回 x
- else //双亲不是自己
- return FIND_SET(t,t[x].parent); //递归在双亲中查找x
- }
- void UNION(UFSTree t[],int x,int y) //将x和y所在的子树合并
- {
- x = FIND_SET(t,x); //查找 x 所在分离集合树的编号
- y = FIND_SET(t,y); //查找 y 所在分离集合树的编号
- if(t[x].rank > t[y].rank) //y 节点的秩小于 x节点的秩
- t[y].parent = x; //将 y 连接到 x 节点上,x 作为 y 的双亲节点
- else{ //y 节点的秩大于等于 x 节点的秩
- t[x].parent = y; //将 x 连接到 y 节点上,y 作为 x 的双亲节点
- if(t[x].rank==t[y].rank) //x 和 y的双亲节点秩相同
- t[y].rank++; //y 节点的秩增 1
- }
- }
- #include <iostream>
- using namespace std;
- /*--------- 并查集 模板 ------------*/
- int UFS_NUM; //并查集中元素总数
- typedef struct node{
- int data; //节点对应的编号
- int rank; //节点对应秩
- int parent; //节点对应的双亲下标
- }UFSTree; //并查集树的节点类型
- void MAKE_SET(UFSTree t[]) //初始化并查集树
- {
- int i;
- for(i=;i<=UFS_NUM;i++){
- t[i].data = i;
- t[i].rank = ;
- t[i].parent = i;
- }
- }
- int FIND_SET(UFSTree t[],int x) //在x所在的子树中查找集合编号
- {
- if(t[x].parent == x)
- return x;
- else
- return FIND_SET(t,t[x].parent);
- }
- void UNION(UFSTree t[],int x,int y) //将x和y所在的子树合并
- {
- x = FIND_SET(t,x);
- y = FIND_SET(t,y);
- if(t[x].rank > t[y].rank)
- t[y].parent = x;
- else{
- t[x].parent = y;
- if(t[x].rank==t[y].rank)
- t[y].rank++;
- }
- }
- /*--------- 判断两线段相交 模板 ------------*/
- const double eps=1e-;
- struct point { double x, y; };
- double min(double a, double b) { return a < b ? a : b; }
- double max(double a, double b) { return a > b ? a : b; }
- bool inter(point a, point b, point c, point d){
- if ( min(a.x, b.x) > max(c.x, d.x) ||
- min(a.y, b.y) > max(c.y, d.y) ||
- min(c.x, d.x) > max(a.x, b.x) ||
- min(c.y, d.y) > max(a.y, b.y) ) return ;
- double h, i, j, k;
- h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
- i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
- j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
- k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
- return h * i <= eps && j * k <= eps;
- }
- /*---------- 代码实现 -----------*/
- struct line
- {
- point p1;
- point p2;
- };
- int main()
- {
- int n;
- UFSTree t[];
- while(cin>>n){
- if(n==) break;
- UFS_NUM = n;//确定并查集树中元素总数
- MAKE_SET(t); //初始化并查集
- line l[];
- for(int i=;i<=n;i++)
- cin>>l[i].p1.x>>l[i].p1.y>>l[i].p2.x>>l[i].p2.y;
- for(int i=;i<=n;i++) //根据关系生成关系树
- for(int j=;j<=n;j++){
- if(i==j) continue;
- if(inter(l[i].p1,l[i].p2,l[j].p1,l[j].p2)){ //如果相交,有亲戚关系
- UNION(t,i,j); //合并相关集合
- }
- }
- int l1,l2;
- while(cin>>l1>>l2){
- if(l1== && l2==)
- break;
- l1 = FIND_SET(t,l1);
- l2 = FIND_SET(t,l2);
- if(l1 == l2)
- cout<<"CONNECTED"<<endl;
- else
- cout<<"NOT CONNECTED"<<endl;
- }
- }
- return ;
- }
poj 1127:Jack Straws(判断两线段相交 + 并查集)的更多相关文章
- TOJ1840: Jack Straws 判断两线段相交+并查集
1840: Jack Straws Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 1 ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- hdu 1147:Pick-up sticks(基本题,判断两线段相交)
Pick-up sticks Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ 1127 Jack Straws(计算几何)
题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...
- You can Solve a Geometry Problem too (hdu1086)几何,判断两线段相交
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3276 ...
- hdu 1086:You can Solve a Geometry Problem too(计算几何,判断两线段相交,水题)
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- hdu 1558 线段相交+并查集
题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...
- [poj 1127]Jack Straws[线段相交][并查集]
题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...
- TTTTTTTTTTTTTT poj 1127 Jack Straws 线段相交+并查集
题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> ...
随机推荐
- 【转】MVC4验证用户登录特性实现方法
在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...
- 关于视频YUV
这里有一篇摘自MSDN的文章.介绍了YUV视频数据格式. About YUV Video Digital video is often encoded in a YUV format. This ar ...
- UNIX网络编程卷2进程间通信读书笔记(二)—管道 (1)
一.管道 管道的名称很形象,它就像是一个水管,我们从一端到水然后水从令一端流出.不同的是这里说的管道的两边都是进程.从一端往管道里写数据,其它进程可以从管道的另一端的把数据读出,从而实现了进程间通信的 ...
- potplayer 网页调用potplayer播放本地视频
网页调用potplayer播放本地视频 CreateTime--2018年1月3日10:36:24 Author:Marydon 源码展示: <!DOCTYPE html> <h ...
- Google C++ Coding Style 学习笔记
写在前面:最新公司马上就要开始开发一款视觉产品,工程量较大,且需要对客户提供可以二次开 发的SDK,整个项目用C++编写. 这就对代码质量提出了非常高的要求,同时,如何设计出优雅稳定的API也是相当大 ...
- linux shell 总结
.#!指定执行脚本的shell 如果不写的话,用系统默认的shell s shell是所有linux ,unix都支持的 .#开始的行表示注释(不限于行首) 命令建议写绝对路径 执行: ./examp ...
- Ubuntu中类似任务管理器的东西?
Ubuntu里面有没有类似windows中任务管理器的东西呢?怎么打开?谢谢!!! ================================ 检举| 2009-02-01 16:50提问者 ...
- 关闭windows打印服务
1.关闭打印服务:开始-运行-services.msc或打开控制面板-管理工具-服务,打开服务列表,找到Print Spooler(打印服务),关闭(右击,点“关闭”).2.删除打印缓存:进入c:\\ ...
- Mysql 创建表和删除表
在数据库中创建一张表的基本语法如下: CREATE TABLE tablename (column_name_1 column_type_1 constraints, column_name_2 co ...
- mongodb查询实练
1.mongodb中如何查询 (a=1 or b=2) and (c=3 or d=4)//格式:db.collection.find({"$and":[{第一个条件},{第二个条 ...