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,但是这个思想还是挺好的,记录下吧

  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. int fa[],n,a,b;
  5. struct Point
  6. {
  7. int x1,x2,y1,y2;
  8. Point(int x1=,int x2=,int y1=,int y2=):x1(x1),x2(x2),y1(y1),y2(y2){}
  9. void read()
  10. {
  11. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  12. }
  13. } p[];
  14. int find(int x)
  15. {
  16. return x==fa[x]?x:fa[x]=find(fa[x]);
  17. }
  18. int cross(int x1,int y1,int x2,int y2)
  19. {
  20. return x1*y2-x2*y1;
  21. }
  22. int la(Point A,Point B)
  23. {
  24. 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 ;
  25. 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);
  26. if(a<=&&b<=)return ;
  27. return ;
  28. }
  29. int main()
  30. {
  31. while(scanf("%d",&n),n)
  32. {
  33. for(int i=; i<=n; i++)
  34. fa[i]=i,p[i].read();
  35. for(int i=; i<n; i++)
  36. for(int j=i+; j<=n; j++)
  37. if(la(p[i],p[j]))
  38. {
  39. a=find(i),b=find(j);
  40. if(a!=b)fa[a]=b;
  41. }
  42. while(scanf("%d%d",&a,&b),a||b)
  43. {
  44. a=find(a),b=find(b);
  45. if(a!=b)printf("NOT ");
  46. printf("CONNECTED\n");
  47. }
  48. }
  49. return ;
  50. }

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. SIGGRAPH 2017:深度学习与计算机图形学的碰撞

    每年由美国计算机协会(Association of Computing Machinery,简称ACM)计算机图形专业组举办的年会SIGGRAPH,是全球最负盛名的图形学和交互技术盛会.今年已经是这场 ...

  2. Windows环境中,通过Charles工具,抓取安卓手机、苹果手机中APP应用的http、https请求包信息

    Windows环境中,通过Charles工具,抓取安卓手机.苹果手机中APP应用的http.https请求包信息1.抓取安卓手机中APP应用的http请求包信息1)在电脑上操作,查看Windows机器 ...

  3. python+selenium之处理HTML5的视频播放

    from selenium import webdriver from time import sleep driver = webdriver.Firefox() driver.get(" ...

  4. CentOS-语言设置

    查看所有的locale语言 # locale -a # locale -a|grep en 查看当前操作系统使用的语言 # echo $LANG 设置系统locale语言为中文环境(永久生效) # v ...

  5. 配置文件无法修改(以修改my-default.ini为例)

    现象: 保存my-default.ini时如果提示“拒绝访问”,右击my-default.ini文件 解决办法: 属性—>安全—>修改权限

  6. 多目标检测分类 RCNN到Mask R-CNN

    最近做目标检测需要用到Mask R-CNN,之前研究过CNN,R-CNN:通过论文的阅读以及下边三篇博客大概弄懂了Mask R-CNN神经网络.想要改进还得努力啊... 目标检测的经典网络结构,顺序大 ...

  7. coredata 删除与更新

    http://blog.csdn.net/rhljiayou/article/details/18037729 //删除 -(void)deleteData { NSManagedObjectCont ...

  8. LibreOffice Writer Comment

    选择文字段后输入:Ctrl+Alt+C可以插入评论,但会出现: unknown author 解决方法:Tools>Options>LibreOffice>User Data. Fi ...

  9. Java字符串池(String Pool)深度解析(转)

    出自  http://www.cnblogs.com/fangfuhai/p/5500065.html 在工作中,String类是我们使用频率非常高的一种对象类型.JVM为了提升性能和减少内存开销,避 ...

  10. cocos2dx 3.x c++代码打包给lua调用过程(mac)

    下载cocos2dx 框架,在应用程序->cocos->framework->cocos2d-x-3.x->tools->tolua目录下,一个ini文件对应一个py文件 ...