POJ1127 Jack Straws
给你一些线段,求出哪些线段是相连的,哪些是不相连的。相连包括间接相连,即这两条线段本身不直接相连,而是通过其它线段的连接而间接相连。
线段相交+并查集
这里主要说如何判断线段相交:快速排斥试验+跨立试验
牢记一点:研究对象一定是两条线段的四个端点

(快速排斥试验:只是可以证明这两条一定不相交,达到一点加速效果)
以线段P1,P2为对角线作一矩形R,再以Q1,Q2为对角线作矩形T,当两个矩形不相交的时候两条线段肯定不相交,即线段相交的必要条件时矩形相交。
矩阵相交依据:两条线段一共四个端点,x/y轴中,最小的两个端点不能属于同一条线段
min(p1.x,p2.x)<=max(q1.x,q2.x)&&
min(q1.x,q2.x)<=max(p1.x,p2.x)&&
min(p1.y,p2.y)<=max(q1.y,q2.y)&&
min(q1.y,q2.y)<=max(p1.y,p2.y)

(跨立试验:叉乘+点乘,保证一条线段的两端点在另一条线段的两侧)
(Q1P1xQ1Q2)*(Q1Q2xQ1P2)>0
(P1Q1xP1P2)*(P1Q2xP1P2)>0
叉乘公式:
a x b=(l,m,n)x(o,p,q)=((mq-pn),(no-lq),(lp-mo))
平面的话 a x b=(a.x,a.y,0)x(b.x,b.y,0)=(0,0,a.x*b.y-a.y-b.x)
上代码:
struct point{
int x,y;
};
struct seg{
point a,b;
}S;
int mul(point p1,p2,p3){
return (p1.x-p2.x)*(p3.y-p2.y)-(p1.y-p2.y)*(p3.x-p2.x);
}
bool insect(point p1,p2,q1,q2){
if(max(p1.x,p2.x)<min(q1.x,q2.x)||max(q1.x,q2.x)<min(p1.x,p2.x)||max((max(p1.y,p2.y)<min(q1.y,q2.y)||max(q1.y,q2.y)<min(p1.y,p2.y))return ;
if(mul(q1,p1,p2)*mul(p2,p1,q2)<=&&mul(p1,q2,q1)*mul(q1,q2,p2)<=)return ;
return ;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void uone(int a,b){
int t1=find(a),t2=find(b);
if(t1==t2)return;
if(rk[t1]>rk[t2])fa[t2]=t1;
else fa[t1]=t2;
if(rk[t1]==rk[t2])rk[t2]++;
}
For(i,,n)
For(j,,i-)
if(find(i)!=find(j)&&insect(seg[i].a,seg[i].b,seg[j].a,seg[j].b))
uone(i,j);
POJ1127 Jack Straws的更多相关文章
- poj1127 Jack Straws(线段相交+并查集)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Jack Straws Time Limit: 1000MS Memory L ...
- 1840: Jack Straws
1840: Jack Straws 时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte 总提交: 168 测试通过:129 描述 I ...
- Jack Straws(poj 1127) 两直线是否相交模板
http://poj.org/problem?id=1127 Description In the game of Jack Straws, a number of plastic or wood ...
- 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 ...
- TOJ 1840 Jack Straws
Description In the game of Jack Straws, a number of plastic or wooden "straws" are dumped ...
- Jack Straws POJ - 1127 (简单几何计算 + 并查集)
In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the table ...
- Jack Straws POJ - 1127 (几何计算)
Jack Straws Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5428 Accepted: 2461 Descr ...
- TOJ1840: Jack Straws 判断两线段相交+并查集
1840: Jack Straws Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 1 ...
随机推荐
- SQLServer 学习笔记之超详细基础SQL语句 Part 6
Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 5------------------- 28 聚合函数 --求平均分 ...
- Hive创建指向HBase表的表
create [external] table t1(id int, value string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorag ...
- EVE Online Third Party Development
第一部分:price_history表 # 建表语句 CREATE TABLE IF NOT EXISTS `price_history` ( `regionID` INT NOT NULL, `ty ...
- Visual Studio Code配置JavaScript环境
一·下载并安装Node.js/Visual Studil Code 下载对应你系统的Node.js版本:https://nodejs.org/en/download/ 选安装目录进行安装 环境配置 · ...
- Oracle EBS 创建 RMA
DECLARE l_api_version_number NUMBER := 1; l_return_status VARCHAR2(2000); l_msg_count NUMBER; l_msg_ ...
- SQL Server如何附加只有mdf的数据库文件
有时候SQL Server意外断电会导致SQL Server的ldf日志文件丢失或者损坏,这个时候你如果直接附加mdf文件到SQL Server会失败,这里提供一个方法可以还原只有mdf的数据库文件, ...
- The current identity ( XXXX) does not have write access to ‘C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files’.解决办法
环境windows 2008+IIS 7 找到 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET FilesTempora ...
- 跨过Django的坑
在最近的Django的学习中,慢慢的开始踩坑,开此栏,专为收纳Django的坑,在以后的学习中以便警示.(使用工具为pycharm专业版2018.2.4,python3.5.2,Django版本2.1 ...
- mysql client之init-command
If the server is a replication master and you want to avoid replicating the content to replication s ...
- 调用webservice 的时候没法输入参数
在web.config的<system.web></system.web>中间加入如下配置节内容<webServices> <protocols> &l ...