题意:

给出一系列线段,判断某两个线段是否连通.

思路:

根据线段相交情况建立并查集, 在同一并查集中则连通.

(第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..)

并查集合并的时候是根节点合并.

快速排斥试验不是必需的, 大规模数据可能是个优化吧.

跨立试验注意共线的情况.

共线判断注意与y 轴平行的情况.

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const double EPS = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 15;
typedef struct node
{
double x,y;
}point;
point a[MAXN],b[MAXN];
bool adj[MAXN][MAXN];
int n; int max(int a, int b)
{
int diff = b - a;
return b - (diff & (diff>>31));
}
int min(int a, int b)
{
int diff = b - a;
return a + (diff & (diff>>31));
}
int fa[MAXN]; int FindSet(int x)
{
if(x==fa[x]) return fa[x];
return fa[x] = FindSet(fa[x]);
} int dcmp(double p)
{
if(fabs(p)<EPS) return 0;
return p>0?1:-1;
} double det(double x1, double y1, double x2, double y2)
{
return x1*y2 - x2*y1;
} double cross(point A, point B, point P)
{
return det(B.x - A.x, B.y - A.y, P.x - A.x, P.y - A.y);
} bool ck(point A, point B, point C, point D)
{
int min1x = min(A.x,B.x), min2x = min(C.x,D.x);
int max1x = max(A.x,B.x), max2x = max(C.x,D.x);
int min1y = min(A.y,B.y), min2y = min(C.y,D.y);
int max1y = max(A.y,B.y), max2y = max(C.y,D.y);
/*if(max(max1x,max2x)-min(min1x,min2x)>(max1x-min1x)+(max2x+min2x) ||
max(max1y,max2y)-min(min1y,min2y)>(max1y-min1y)+(max2y+min2y)) return false;*/
int a = dcmp(cross(A, B, C));
int b = dcmp(cross(A, B, D));
if(!a && !b)
return ( max(max1x,max2x)-min(min1x,min2x)<=(max1x-min1x)+(max2x-min2x)
&& max(max1y,max2y)-min(min1y,min2y)<=(max1y-min1y)+(max2y-min2y) );
int c = dcmp(cross(C, D, A));
int d = dcmp(cross(C, D, B));
return (( a* b <= 0) && ( c* d <= 0));//规范或不规范相交
} void BuildSet()
{
for(int i=1;i<=n;i++)
{
fa[i] = i;
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(ck(a[i],b[i],a[j],b[j]))
{
int fi = FindSet(i), fj = FindSet(j);//并查集写错了= =
fa[fi] = fj;
}
}
}
} bool check(int a, int b)
{
return (FindSet(a)==FindSet(b));
} int main()
{
int x, y;
while(scanf("%d",&n)==1 && n)
{
for(int i=1;i<=n;i++)
scanf("%lf %lf %lf %lf",&a[i].x,&a[i].y,&b[i].x,&b[i].y);
BuildSet();
while(scanf("%d %d",&x,&y)==2 && (x+y))
{
if(check(x, y)) printf("CONNECTED\n");
else printf("NOT CONNECTED\n");
}
} }

[poj 1127]Jack Straws[线段相交][并查集]的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 1127 Jack Straws(计算几何)

    题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...

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

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

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

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

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

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

随机推荐

  1. VM中装Linux换ISO文件报错"该光盘无法被挂载"

    一.发现问题 利用VM安装Red Hat Linux的时候,第一个iso安装完毕,准备换第二个iso,报错“该光盘无法被挂载”. 二.解决办法 上面的菜单栏中“虚拟机”—>“设置”—>“硬 ...

  2. asp.net2.0安全性(1)--用户角色篇(起篇)--转载来自车老师

    安全管理的解决方案在.net1.1中几乎为一片空白,对于应用程序的验证与授权大部分的工作是开发人员自己编写代码,或者是借助企业库等工具来实现,此可谓.net1.1中的一大缺憾.在.net2.0中微软为 ...

  3. linux命令:rm

    删文件要一个个回答y,谁有好办法自动删除? rm -rf 用rm递归删除目录下面的所有.o文件: find . -name "*.o"  | xargs rm -f :

  4. AJAX实现类似百度的搜索提示,自动补全和键盘、鼠标操作

    <script type="text/javascript"> $(document).ready(function(){ var highlightIndex = - ...

  5. RStudio版本号管理 整合Git

    本文为原创,转载注明出处. 系统环境: win7 x64 R-3.1.0-win.exe RStudio-0.98.507.exe 前置条件:必须拥有github仓库: 如:https://githu ...

  6. Android:解决client从server上获取数据乱码的方法

    向server发送HTTP请求.接收到的JSON包为response,用String content = EntityUtils.toString(response.getEntity()," ...

  7. KMP原理、分析及C语言实现

    (是在matrix67博客基础上整理而来,整理着:华科小涛@http://www.cnblogs.com/hust-ghtao/) 有些算法可以让人发疯,KMP算法就是一个.在网上找了很多资料讲的都让 ...

  8. cp命令的实现

    #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE 4096 ...

  9. ASP.NET - 分页

    效果: SQL-存储过程(Paging): ROW_NUMBER() over(order by MessageDateTime desc)  其中的 MessageDateTime desc 代表的 ...

  10. QNX 多线程 (线程1每隔20ms读取 number;线程2每隔10ms计算一次)

    #include <pthread.h>#include <stdio.h>#include <sys/time.h>#include <string.h&g ...