Jack Straws

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

题目大意:按顺序输入n个线段的两个坐标,然后多组输入判断两个线段是否是连接的(相交即为连接)。

题解:利用计算几何的知识,建立线段,如果有线段相交,就用并查集把它们连在一起,然后判断根节点是不是一个就好啦。比较简单的模板题目。

有问题欢迎━(*`∀´*)ノ亻!指出!

#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
double x,y;
};
struct d
{
node a;
node b;
}deline[];
double cross(node a,node b,node o)
{
return(a.x-o.x)*(b.y-o.y)-(b.x-o.x)*(a.y-o.y);
}
bool isxj(d u,d v)
{
return (cross(v.a,u.b,u.a)*cross(u.b,v.b,u.a)>=)&&
(cross(u.a,v.b,v.a)*cross(v.b,u.b,v.a)>=)&&
(max(u.a.x,u.b.x)>=min(v.a.x,v.b.x))&&
(max(v.a.x,v.b.x)>=min(u.a.x,u.b.x))&&
(max(u.a.y,u.b.y)>=min(v.a.y,v.b.y))&&
(max(v.a.y,v.b.y)>=min(u.a.y,u.b.y));
}
int father[];
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}
void mix(int x,int y)
{
int xx=find(x),yy=find(y);
if(xx!=yy)
{
father[xx]=yy;
}
}
int main(){
int n;
while(scanf("%d",&n),n)
{
for(int i=;i<;i++)
{
father[i]=i;
}
for(int i=;i<=n;i++)
{
scanf("%lf %lf %lf %lf",&deline[i].a.x,&deline[i].a.y,&deline[i].b.x,&deline[i].b.y);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(isxj(deline[i],deline[j]))
{
mix(i,j);
}
}
}
int a,b;
while(scanf("%d%d",&a,&b),a&&b)
{
if(find(a)==find(b))
printf("CONNECTED\n");
else
printf("NOT CONNECTED\n");
}
}
}

poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)的更多相关文章

  1. poj 1127:Jack Straws(判断两线段相交 + 并查集)

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

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

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

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

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

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

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

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

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

  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. 定时延时设计FPGA

    以50MHZ时钟为例,进行1秒钟延时,并输出延时使能信号. 首先计算需要多少次计时,MHZ=10的六次方HZ.T=20ns 一秒钟需要计时次数为5的七次方即5000_0000. 然后计算需要几位的寄存 ...

  2. WPF:window设置单一开启

    方法一: Window window = new Window();window.ShowDialog; 方法二: 设置一个判断窗口打开状态的全局控制变量         private bool i ...

  3. 一个基于TCP/IP的服务器与客户端通讯的小项目(超详细版)

    1.目的:实现客户端向服务器发送数据 原理: 2.建立两个控制台应用,一个为服务器,用于接收数据.一个为客户端,用于发送数据. 关键类与对应方法: 1)类IPEndPoint: 1.是抽象类EndPo ...

  4. A human being,who loves football and music

    ---title: aboutdate: 2019-08-09 20:52:27---[A human being,who loves football and music.](https://eel ...

  5. TI MSP430工程配置及2019年电赛A题编程示例(使用430 F5529)

    配置 第一步:右击工程,选择Options 第二步:在General Options的Target选项卡里选择对应的器件Device,这里是MSP430G2231 第三步:在Debugger里选择FE ...

  6. Scala集合(四)

    1. 集合 集合主要有三种: Sequence Map Set sequence是一种线性元素的集合,可能会是索引或者线性的(链表).map是包含键值对的集合,就像Java的Map,set是包含无重复 ...

  7. PHP版本的区别与用法详解

    在我们安装PHP模块时,有时需要注意PHP编译的版本,下面讲解下PHP中VC6.VC9.TS.NTS版本的区别与用法详解,介绍php的两种执行方式. 1. VC6与VC9的区别:VC6版本是使用Vis ...

  8. java Timer工具类实现定时器任务

    第一 schedule 方法 三个参数 按照顺序 (执行的任务方法,开始执行时间,多少时间后循环去执行)  代码可用 public class TestScheedule { public stati ...

  9. 测试自动化:java+selenium3 UI自动化(1) - 环境搭建

    1.前言 我大概是在2012年第一次正式接触到自动化测试,那个时候跟随我的团队一起,就当时项目的UI自动化尝试做出了探索. 在我离开那家公司的时候,我们的自动化测试体系仍然难言完美,但是也已经达到了非 ...

  10. Scrum Master如何让敏捷团队正常运转?

    官方<Scrum指南>中定义:Scrum Master在Scrum团队中属于服务型领导,负责践行和支持<Scrum指南>中定义的Scrum,要帮团队的每个人理解Scrum理论. ...