Jack Straws
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2911   Accepted: 1322

Description

In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table and players try to remove them one-by-one without disturbing the other straws. Here, we are only concerned with if various pairs of straws are connected by a path of touching straws. You will be given a list of the endpoints for some straws (as if they were dumped on a large piece of graph paper) and then will be asked if various pairs of straws are connected. Note that touching is connecting, but also two straws can be connected indirectly via other connected straws.

Input

Input consist multiple case,each case consists of multiple lines. The first line will be an integer n (1 < n < 13) giving the number of straws on the table. Each of the next n lines contain 4 positive integers,x1,y1,x2 and y2, giving the coordinates, (x1,y1),(x2,y2) of the endpoints of a single straw. All coordinates will be less than 100. (Note that the straws will be of varying lengths.) The first straw entered will be known as straw #1, the second as straw #2, and so on. The remaining lines of the current case(except for the final line) will each contain two positive integers, a and b, both between 1 and n, inclusive. You are to determine if straw a can be connected to straw b. When a = 0 = b, the current case is terminated.

When n=0,the input is terminated.

There will be no illegal input and there are no zero-length straws.

Output

You should generate a line of output for each line containing a pair a and b, except the final line where a = 0 = b. The line should say simply "CONNECTED", if straw a is connected to straw b, or "NOT CONNECTED", if straw a is not connected to straw b. For our purposes, a straw is considered connected to itself.

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


 
  判断两线段相交 + 并查集
  一开始以为是简单的判断两线段相交问题,提交WA,后来发现还要用到并查集,因为这道题允许2条线段通过其他线段间接的相交,这就要求通过亲戚关系查找2条线段是否在同一集合。我一看正好是下学期数据结构的知识,就趁机熟悉了一遍。自己敲上了并查集的模板,判断两线段相交直接套用了模板,提交AC。
 
  并查集模板
 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(判断两线段相交 + 并查集)的更多相关文章

  1. TOJ1840: Jack Straws 判断两线段相交+并查集

    1840: Jack Straws  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: 1 ...

  2. poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)

    Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...

  3. hdu 1147:Pick-up sticks(基本题,判断两线段相交)

    Pick-up sticks Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. POJ 1127 Jack Straws(计算几何)

    题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...

  5. 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 ...

  6. 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 ...

  7. hdu 1558 线段相交+并查集

    题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...

  8. [poj 1127]Jack Straws[线段相交][并查集]

    题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...

  9. TTTTTTTTTTTTTT poj 1127 Jack Straws 线段相交+并查集

    题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> ...

随机推荐

  1. [转发]MVC WebAPI get和post请求

    转自:http://www.cnblogs.com/babycool/p/3922738.html 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用J ...

  2. CentOS7——gitlab本地git仓库搭建 以及web hook配置

    整个搭建用的都是各种默认设置,所以没有用到高深的的东西,比较简单,比较傻瓜式,这篇也仅仅是一个入门. 另外本文具有时效性,浏览本文请注意发表时间,为防止过时产生误导,本文尽量把 “如何得知应该这样做” ...

  3. 微信小程序弹窗组件

    概述 自己封装的一个比较简单微信弹窗小组件,主要就是教会大家对微信小组件的用法和理解,因为微信小程序对组件介绍特别少,所以我就把自己的理解分享给大家 详细 代码下载:http://www.demoda ...

  4. Thrift实现C#调用Java开发步骤详解

    概述 Thrift实现C#调用Java开发步骤详解 详细 代码下载:http://www.demodashi.com/demo/10946.html Apache Thrift 是 Facebook ...

  5. nyoj847 S + T(贪心)

    题目847 题目信息 执行结果 本题排行 讨论区 S + T 时间限制:1000 ms  |  内存限制:65535 KB 难度: 描写叙述 给你一个长度为n的整数序列A1.A2,--,An,找出两个 ...

  6. spring注解 annotation

    @Resourse(name="  xxx") 意味从上下文找xxx名字一样的然后引入 @Repository("personDao") 意味生成一个 bean ...

  7. Ubuntu中类似任务管理器的东西?

    Ubuntu里面有没有类似windows中任务管理器的东西呢?怎么打开?谢谢!!!   ================================ 检举| 2009-02-01 16:50提问者 ...

  8. Ubuntu下安装使用Xfce4

    编辑于 2007-05-05 21:30   安装:  代码: sudo  apt-get  install  xfce4  xfce4-taskbar-plugin     (xfce4-taskb ...

  9. idea 更换编辑器背景图片

    插件名称是:BackgroundImage, 安装后效果图

  10. 随笔小问题(一)--mac打开class文件

    本来不想写这个东西的.但是这个却费了我一番周折. 我要先声明一点的是,我从来不讲iOS当成一个单独的系统,而是将这个操作系统归位unix内核的系统. 简单来说,我把它当成linux在用. 但是,mac ...