1840: Jack Straws 

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 154            Accepted:119

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

East Central North America 1994

我的代码不优秀啊,卡不到0ms,但是这个思想还是挺好的,记录下吧

#include<stdio.h>
#include<algorithm>
using namespace std;
int fa[],n,a,b;
struct Point
{
int x1,x2,y1,y2;
Point(int x1=,int x2=,int y1=,int y2=):x1(x1),x2(x2),y1(y1),y2(y2){}
void read()
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
}
} p[];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int cross(int x1,int y1,int x2,int y2)
{
return x1*y2-x2*y1;
}
int la(Point A,Point B)
{
if(max(A.x1,A.x2)<min(B.x1,B.x2)||max(A.y1,A.y2)<min(B.y1,B.y2)||max(B.x1,B.x2)<min(A.x1,A.x2)||max(B.y1,B.y2)<min(A.y1,A.y2)) return ;
int a=cross(A.x2-A.x1,A.y2-A.y1,B.x1-A.x1,B.y1-A.y1)*cross(A.x2-A.x1,A.y2-A.y1,B.x2-A.x1,B.y2-A.y1),b=cross(B.x2-B.x1,B.y2-B.y1,A.x1-B.x1,A.y1-B.y1)*cross(B.x2-B.x1,B.y2-B.y1,A.x2-B.x1,A.y2-B.y1);
if(a<=&&b<=)return ;
return ;
}
int main()
{
while(scanf("%d",&n),n)
{
for(int i=; i<=n; i++)
fa[i]=i,p[i].read();
for(int i=; i<n; i++)
for(int j=i+; j<=n; j++)
if(la(p[i],p[j]))
{
a=find(i),b=find(j);
if(a!=b)fa[a]=b;
}
while(scanf("%d%d",&a,&b),a||b)
{
a=find(a),b=find(b);
if(a!=b)printf("NOT ");
printf("CONNECTED\n");
}
}
return ;
}

TOJ1840: Jack Straws 判断两线段相交+并查集的更多相关文章

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

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

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

  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. hdu 1558 线段相交+并查集

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

  7. TZOJ 1840 Jack Straws(线段相交+并查集)

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

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

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

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

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

随机推荐

  1. 投资20万研发的JFinal项目《旅游线路营销管理系统》准备公开课中

    18年初上线了一套旅游营销管理系统,目前给几个合作客户内测试用,是基于JFinal研发的一套旅游行业旅游线路批发零售系统(SAAS)版. 系统终端: PC后台管理分:总部.线路批发商.旅行社门店.个人 ...

  2. python 之正则表达式

    一.正则表达式 首先,我们需要感性的了解下什么是正则表达式,简单的是说“正则表达式”就是一个“表达式”,更准确定义是:“用一个简洁的方法来实现对“一组字符串”的表达式. 最终目的就是实现“一行胜千言” ...

  3. ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决【转】

    编写Winform程序,遇到comboBox的绑定事件和索引项变更事件的冲突问题,就是“设置 DataSource 属性后无法修改项集合”的错误问题,网上查了很多,大多说在索引项变更是进行非空判断,还 ...

  4. python爬虫之路——初识爬虫三大库,requests,lxml,beautiful.

    三大库:requests,lxml,beautifulSoup. Request库作用:请求网站获取网页数据. get()的基本使用方法 #导入库 import requests #向网站发送请求,获 ...

  5. [机器视觉] SIFT特征-尺度不变特征理解

    SIFT特征-尺度不变特征理解 简介 SIFT,即尺度不变特征变换(Scale-invariant feature transform,SIFT),是用于图像处理领域的一种描述.这种描述具有尺度不变性 ...

  6. [Java] 新手快速就业需要掌握的知识点

    目的:主要是分享下日常工作中使用到的技术点,根据二八定律快速掌握使用知识点,先就业再沉淀去积累经验.(个人建议仅供参考) 背景:目前一般来说,都是前后端分离.你只需要提供接口给前端,他来处理就可以了, ...

  7. mysql 速度检索

    授权GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTI ...

  8. HTML_5 (1 2 3的代码总结)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 《毛毛虫团队》第九次团队作业:BETA冲刺与团队项目验收

    一:实验名称:Beta冲刺与验收准备 二:实验目的与要求 (1)掌握软件黑盒测试技术: (2)学会编制软件项目总结PPT.项目验收报告: (3)掌握软件项目验收内容,验收流程. 三.实验内容与步骤 任 ...

  10. sessionStorage对象

    sessStorage对象是一个会话形式的数据存储,当用户关闭浏览器的窗口后,数据就会被删除. 实例: <!DOCTYPE html><html><head> &l ...