描述

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个询问,两条线是否连通

题解

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

代码

  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. int f[];
  6. int Find(int x)
  7. {
  8. int r=x;
  9. while(f[r]!=r)
  10. r=f[r];
  11. int i=x,j;
  12. while(i!=r)
  13. {
  14. j=f[i];
  15. f[i]=r;
  16. i=j;
  17. }
  18. return r;
  19. }
  20. void join(int x,int y)
  21. {
  22. int a,b;
  23. a=Find(x);
  24. b=Find(y);
  25. if(a!=b)
  26. f[a]=b;
  27. }
  28. void init()
  29. {
  30. for(int i=;i<=;i++)
  31. f[i]=i;
  32. }
  33.  
  34. struct point{double x,y;};
  35. bool judge(point a,point b,point c,point d)
  36. {
  37. //用矩形是否重叠快速排斥(共线能通过跨立不行的要排掉)
  38. if(!(min(a.x,b.x)<=max(c.x,d.x)&&min(c.y,d.y)<=max(a.y,b.y)
  39. &&min(c.x,d.x)<=max(a.x,b.x)&&min(a.y,b.y)<=max(c.y,d.y)))
  40. return false;
  41. //跨立
  42. double u,v,w,z;
  43. u=(c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);//u为正说明cb在ab的顺时针
  44. v=(d.x-a.x)*(b.y-a.y)-(b.x-a.x)*(d.y-a.y);//db和ab
  45. w=(a.x-c.x)*(d.y-c.y)-(d.x-c.x)*(a.y-c.y);//ad和cd
  46. z=(b.x-c.x)*(d.y-c.y)-(d.x-c.x)*(b.y-c.y);//bd和cd
  47. return (u*v<=0.00000001&&w*z<=0.00000001);
  48. }
  49.  
  50. struct line
  51. {
  52. point p1,p2;
  53. }l[];
  54. int main()
  55. {
  56. int n;
  57. while(scanf("%d",&n)!=EOF,n)
  58. {
  59. init();
  60. for(int i=;i<=n;i++)
  61. {
  62. scanf("%lf%lf%lf%lf",&l[i].p1.x,&l[i].p1.y,&l[i].p2.x,&l[i].p2.y);
  63. }
  64. for(int i=;i<=n;i++)
  65. {
  66. for(int j=;j<=n;j++)
  67. {
  68. if(i==j)continue;
  69. if(judge(l[i].p1,l[i].p2,l[j].p1,l[j].p2))
  70. join(i,j);
  71. }
  72. }
  73. int a,b;
  74. while(scanf("%d%d",&a,&b)!=EOF,a||b)
  75. {
  76. a=Find(a);
  77. b=Find(b);
  78. if(a==b)
  79. printf("CONNECTED\n");
  80. else
  81. printf("NOT CONNECTED\n");
  82. }
  83. }
  84. return ;
  85. }

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. 搭建OpenStack先电云平台

    实际操作示意图 在VMware里面创建两台centos7的虚拟机作为搭建云平台的两节点配置如下: 1.第一台虚拟机   作为控制节点 2CPU 3G以上内存 硬盘50G 网络适配器一个nat 一个仅主 ...

  2. ros建立ospf邻居的条件

    Two routers do not become neighbors unless the following conditions are met. Two way communication b ...

  3. CA证书认证单向和双向的区别

     我觉得最科学的应该是,单向的,每次客户端发两把锁住的东西给服务端,服务端解密两次,服务端用客户端发来的对称密钥加密数据,发送给客户端,客户端只需解密一次,然后客户端每次修改随机密码,传给服务端,服务 ...

  4. freePBX汉化方法记录——备忘

    FreePBX汉化[root@bgcc69:/var/www/html/admin/i18n/zh_CN/LC_MESSAGES]$pwd/var/www/html/admin/i18n/zh_CN/ ...

  5. R语言中的遗传算法详细解析

    前言 人类总是在生活中摸索规律,把规律总结为经验,再把经验传给后人,让后人发现更多的规规律,每一次知识的传递都是一次进化的过程,最终会形成了人类的智慧.自然界规律,让人类适者生存地活了下来,聪明的科学 ...

  6. python之路之迭代器与生成器

    一  迭代器 那么在研究迭代器之前首先应该要知道什么是迭代. 迭代:是一个重复的过程,并且每次重复都是建立基于上一次的结果而来的,所以在迭代的过程其实是在不断变化的. 迭代器:就是迭代取值的工具. 那 ...

  7. django-类装饰器method_decorator

    import os from django.shortcuts import render from django.contrib.admin.views.decorators import staf ...

  8. IP分组交付和转发

    1:交付 网络层监视底层物理网络对分组的处理过程叫做交付,分为直接交付和间接交付 1.1:直接交付 直接交付时,分组的终点是一台与交付着连接在同一个网络上的主机,发生在俩种情况下,分组的源点和终点都在 ...

  9. python入门-用户输入

    1 input()函数来实现用户输入,程序在等待输入的时候会终止,获取用户的输入后继续 message = input("tell me something,and I will repre ...

  10. php读取word里面的内容antiword

    其实是现在一个linux下的扩展 1 先安装  antiword yum antiword install 2 写测试php代码 header("Content-type: text/htm ...