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<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e3+;
const int mod = 1e9+; struct node{
double x,y;
};
struct Line{
node a;
node b;
}line[];
int father[];
void init()
{
for(int i=;i<;i++)
father[i] = i;
}
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 connect(Line u,Line 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 find(int x)
{
return x == father[x] ? x : father[x] = find(father[x]);
}
void combine(int x,int y)
{
x = find(x);
y = find(y);
if(x != y)
father[x] = y;
}
int main()
{
int n;
while(scanf("%d",&n) && n)
{
init();
for(int i=;i<=n;i++)
scanf("%lf %lf %lf %lf",&line[i].a.x,&line[i].a.y,&line[i].b.x,&line[i].b.y);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(connect(line[i],line[j]))
combine(i,j);
int a,b;
while(scanf("%d %d",&a,&b) && a+b)
{
if(find(a) == find(b))
puts("CONNECTED");
else
puts("NOT CONNECTED");
}
}
}

Jack Straws POJ - 1127 (简单几何计算 + 并查集)的更多相关文章

  1. Jack Straws POJ - 1127 (几何计算)

    Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5428   Accepted: 2461 Descr ...

  2. Jack Straws(POJ 1127)

    原题如下: Jack Straws Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5555   Accepted: 2536 ...

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

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

  4. poj 1127(直线相交+并查集)

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

  5. Jack Straws(判断线段是否相交 + 并查集)

    /** http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1840    题意:    判断线段 ...

  6. poj 2236:Wireless Network(并查集,提高题)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 16065   Accepted: 677 ...

  7. LA3027简单带权并查集

    题意:       有n个点,一开始大家都是独立的点,然后给出一些关系,a,b表示a是b的父亲节点,距离是abs(a-b)%1000,然后有一些询问,每次询问一个节点a到父亲节点的距离是多少? 思路: ...

  8. POJ 1611 The Suspects(并查集,简单)

    为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了? 题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数. 这是简单并查集应用,所以直接看代码吧! ...

  9. poj 2492 a bug's life 简单带权并查集

    题意大致为找同性恋的虫子.... 这个比食物链要简单些.思路完全一致,利用取余操作实现关系之间的递推. 个人感觉利用向量,模和投影可能可以实现具有更加复杂关系的并查集. #include<ios ...

随机推荐

  1. 内容显示分页数字分页 aspx

    此处是aspx里面分页显示,数据层和业务层是由动软生成 当然,我们也可以可以利用listView实现分页ListView(高效分页) public partial class NewList : Sy ...

  2. JavaSE之Java基础(5)

    21.简述正则表达式及其用途. 在编写处理字符串的程序时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 22.Java ...

  3. 用简单的方法学习ES6

    ES6 简要概览 这里是ES6 简要概览.本文大量参考了ES6特性代码仓库,请允许我感谢其作者@Luke Hoban的卓越贡献,也感谢@Axel Rauschmayer所作的[优秀书籍]//explo ...

  4. CentOS 6.4系统中编译和升级内核

    CentOS 6.4系统中编译和升级内核 [日期:2013-08-25] 来源:Linux社区  作者:vipshichg [字体:大 中 小] 可能因为以下几种原因,你可能需要对Linux kern ...

  5. 玩转spring ehcache 缓存框架

    一.简介 Ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache ...

  6. php 02

    变量的数据类型 一.类型 标量类型: 布尔型 整型 浮点型 字符串 复合类型:  数组  对象 特殊类型:  资源 null 1. 布尔型 true  false 以下值认为是false  其他值都认 ...

  7. HTTPS与SSL(一)

    1.  HTTPS HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版 ...

  8. 【extjs6学习笔记】1.12 初始: Working with DOM

    http://www.extjs-tutorial.com/extjs/working-with-dom Ext JS是一个DHTML库. 它通过使用JavaScript创建或操作DOM元素来创建UI ...

  9. HDU3371 Connect the Cities

    题目描述: 有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的.现在想把所有的岛都连通起来,求最少的花费是多少. 输入: 第一行输入T,代表多少组数据. ...

  10. 因技术垃圾直接上手groovy的工作感悟

    因为熟悉devops,用IDEA的git完成版本提交,不太喜欢公司的代码控制,从事groovy基本没有代码控制,提交就打个压缩包给指定的人. 没有持续继承,持续部署(CICD). http://xio ...