[poj 1127]Jack Straws[线段相交][并查集]
题意:
给出一系列线段,判断某两个线段是否连通.
思路:
根据线段相交情况建立并查集, 在同一并查集中则连通.
(第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..)
并查集合并的时候是根节点合并.
快速排斥试验不是必需的, 大规模数据可能是个优化吧.
跨立试验注意共线的情况.
共线判断注意与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[线段相交][并查集]的更多相关文章
- TTTTTTTTTTTTTT poj 1127 Jack Straws 线段相交+并查集
题意: 有n个木棍,给出木棍的两个端点的x,y坐标,判断其中某两个线段是否连通(可通过其他线段连通) #include <iostream> #include <cstdio> ...
- POJ 1127 Jack Straws (线段相交)
题意:给定一堆线段,然后有询问,问这两个线段是不是相交,并且如果间接相交也可以. 析:可以用并查集和线段相交来做,也可以用Floyd来做,相交就是一个模板题. 代码如下: #pragma commen ...
- poj1127 Jack Straws(线段相交+并查集)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Jack Straws Time Limit: 1000MS Memory L ...
- TZOJ 1840 Jack Straws(线段相交+并查集)
描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...
- poj 1127:Jack Straws(判断两线段相交 + 并查集)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2911 Accepted: 1322 Descr ...
- POJ 1127 Jack Straws(计算几何)
题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...
- poj 1127 -- Jack Straws(计算几何判断两线段相交 + 并查集)
Jack Straws In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- TOJ1840: Jack Straws 判断两线段相交+并查集
1840: Jack Straws Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 1 ...
- hdu 1558 线段相交+并查集
题意:要求相交的线段都要塞进同一个集合里 sol:并查集+判断线段相交即可.n很小所以n^2就可以水过 #include <iostream> #include <cmath> ...
随机推荐
- [置顶] DX报表的开发流程(1)
DevExpress.XtraReports简称为DX报表. 本文主要介绍下VS2010项目中如何开发DX报表,具体的内容包括: (1)下载安装 (2)报表文件创建 (3)页面文件的创建 (4)页面的 ...
- 重操JS旧业第六弹:基本类型包装
在前面已经知道js中的类型有boolean,string,number,undefined,function,object,其中boolean,number,string为值类型.所谓的基本类型包装, ...
- C# LDAP认证登录
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据需要定制.与X ...
- Logistic Regression(逻辑回归)(二)—深入理解
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 上一篇讲解了Logistic Regression的基础知识,感觉 ...
- PHP开发-上传文件
<?php /****************************************************************************** 参数说明: $max_ ...
- HDU4712-----Hamming Distance------超级大水题
本文出自:http://blog.csdn.net/dr5459 题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4712 题目意思: 海明距离:任意两个 ...
- ASP.NET - 获得客户端的 IP 地址
通常我们都通过下面的代码获得IP: REMOTE_ADDR 说明:访问客户端的 IP 地址. 此项信息用户不可以修改.如果真的给改了的话,你也和服务器连接不了了,服务器就是按照这个来与客户端建立连接并 ...
- Net Core子应用由于配置引起IIS错误500.19
Asp.Net Core子应用由于配置中重复添加模块会引起IIS错误500.19 ASP.NET Core已经从IIS中解耦,可以作为自宿主程序运行,不再依赖IIS. 但我们还是需要强大的IIS作为前 ...
- delphi 网页提交按钮执行点击事件
遍历即可实现,下列代码仅供参考: var i: integer; T: OleVariant; begin T := WebBrowser1.Document; do begin if T.all.i ...
- perl 使用cookie
use Net::SMTP; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Enc ...