描述

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

输出

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.

样例输入

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

样例输出

CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
NOT CONNECTED
CONNECTED
CONNECTED
CONNECTED
CONNECTED

题意

给你n条线的两个端点坐标,有m个询问,两条线是否连通

题解

首先判断任意两条线是否相交,再把相交的加入并查集

代码

 #include<stdio.h>
#include<algorithm>
using namespace std; int f[];
int Find(int x)
{
int r=x;
while(f[r]!=r)
r=f[r];
int i=x,j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void join(int x,int y)
{
int a,b;
a=Find(x);
b=Find(y);
if(a!=b)
f[a]=b;
}
void init()
{
for(int i=;i<=;i++)
f[i]=i;
} struct point{double x,y;};
bool judge(point a,point b,point c,point d)
{
//用矩形是否重叠快速排斥(共线能通过跨立不行的要排掉)
if(!(min(a.x,b.x)<=max(c.x,d.x)&&min(c.y,d.y)<=max(a.y,b.y)
&&min(c.x,d.x)<=max(a.x,b.x)&&min(a.y,b.y)<=max(c.y,d.y)))
return false;
//跨立
double u,v,w,z;
u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);//u为正说明cb在ab的顺时针
v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);//db和ab
w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);//ad和cd
z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);//bd和cd
return (u*v<=0.00000001&&w*z<=0.00000001);
} struct line
{
point p1,p2;
}l[];
int main()
{
int n;
while(scanf("%d",&n)!=EOF,n)
{
init();
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&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(judge(l[i].p1,l[i].p2,l[j].p1,l[j].p2))
join(i,j);
}
}
int a,b;
while(scanf("%d%d",&a,&b)!=EOF,a||b)
{
a=Find(a);
b=Find(b);
if(a==b)
printf("CONNECTED\n");
else
printf("NOT CONNECTED\n");
}
}
return ;
}

TZOJ 1840 Jack Straws(线段相交+并查集)的更多相关文章

  1. poj1127 Jack Straws(线段相交+并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Jack Straws Time Limit: 1000MS   Memory L ...

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

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

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

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

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

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

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

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

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

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

  7. POJ 1127 Jack Straws (线段相交)

    题意:给定一堆线段,然后有询问,问这两个线段是不是相交,并且如果间接相交也可以. 析:可以用并查集和线段相交来做,也可以用Floyd来做,相交就是一个模板题. 代码如下: #pragma commen ...

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

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

  9. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

随机推荐

  1. 1027 Colors in Mars (20 分)

    1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way a ...

  2. centos 7.5 安装mongodb

    MongoDB安装和启动 从官网下载最新对应的版本然后解压,本文以3.6.9为例,将文件拷贝到opt目录下,然后解压: [root@localhost opt]# tar zxvf mongodb-l ...

  3. 【Python编程:从入门到实践】chapter6 字典

    chapter6 字典 6.1 一个简单的字典 6.2 使用字典 6.2.1 访问字典中的值 6.2.2 添加键值对 6.2.3 先创建一个空字典 6.2.4 修改字典中的值 6.2.5 删除键值对 ...

  4. Samba 简介

    SMB 代表的是服务器消息块 (Server Message Block),它是用于在 Windows 上共享文件的协议的原始名称. CIFS 代表公共 Internet 文件系统 (Common I ...

  5. solr入门之权重排序方法初探之使用edismax改变权重

    做搜索引擎避免不了排序问题,当排序没有要求时,solr有自己的排序打分机制及sorce字段 1.无特殊排序要求时,根据查询相关度来进行排序(solr自身规则) 2.当涉及到一个字段来进行相关度排序时, ...

  6. mysql更新(四) 数据类型

    07-数据类型   介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 详细参考链接:http://www.runoob.com/mysql/m ...

  7. 20180130之PYTHON学习笔记【PYTHON3写个自动听课功能】

    -----------------------原始实现想法------------ import pyautoguifrom PIL import Image#img=Image.open('c:/p ...

  8. PHP闭包

    # 提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它.声明一个匿名函数是这样: $func = function() {       }; //带结束符 ...

  9. Overriding managed version XX for YY

    在警告部分,添加<!--$NO-MVN-MAN-VER$-->. <build> <plugins> <plugin> <groupId>o ...

  10. 在 WampServer 上手工安装 PHP 的多个版本

    手工安装新版本的PHP,只需以下步骤: 下载要安装的PHP版本.既然是用WampServer,那当然是下载Window版本的ZIP包啦:http://windows.php.net.解压到 Wamp的 ...