Segment set

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3548    Accepted Submission(s): 1324

Problem Description
A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set.



 
Input
In the first line there is an integer t - the number of test case. For each test case in first line there is an integer n (n<=1000) - the number of commands. 



There are two different commands described in different format shown below:



P x1 y1 x2 y2 - paint a segment whose coordinates of the two endpoints are (x1,y1),(x2,y2).

Q k - query the size of the segment set which contains the k-th segment.



k is between 1 and the number of segments in the moment. There is no segment in the plane at first, so the first command is always a P-command.
 
Output
For each Q-command, output the answer. There is a blank line between test cases.
 
Sample Input
1
10
P 1.00 1.00 4.00 2.00
P 1.00 -2.00 8.00 4.00
Q 1
P 2.00 3.00 3.00 1.00
Q 1
Q 3
P 1.00 4.00 8.00 2.00
Q 2
P 3.00 3.00 6.00 -2.00
Q 5
 
Sample Output
1
2
2
2
5
 
Author
LL
 

主要可能会直线重叠的情况,此时也算相交

所以若叉积为0 点积也为0时也算相交

代码如下:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define exp 0.000001
using namespace std;
int N,tot;
struct point
{
double x,y;
};
point S[10110],E[10011];
int Set[10110];
int ANS[10110];
int sgn(double x)
{
if(fabs(x)<exp) return 0;
else if(x<0) return -1;
else return 1;
}
double dotdet(double x1,double y1,double x2,double y2)
{
return x1*x2+y1*y2;
}
double dot(point a,point b,point c)
{
return dotdet(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
double crossdet(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point a,point b,point c)
{
return crossdet(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);
}
double pan(point s1,point e1,point s2,point e2)
{
//跨立实验
double a1=cross(s1,e1,s2); //s1-e1 s2
double a2=cross(s1,e1,e2);
double a3=cross(s2,e2,s1);
double a4=cross(s2,e2,e1);
if(sgn(a1*a2)<0&&sgn(a3*a4)<0) return 1;
if((a1==0&&sgn(dot(s2,s1,e1))<=0)||
(a2==0&&sgn(dot(e2,s1,e1))<=0)||
(a3==0&&sgn(dot(s1,s2,e2))<=0)||
(a4==0&&sgn(dot(e1,s2,e2))<=0))
return 1;
return 0;
}
void CSH()
{
for(int i=0;i<=10001;i++)
{
Set[i]=i;
ANS[i]=1;
}
tot=0;
}
int find(int x)
{
if(x!=Set[x])
Set[x]=find(Set[x]);
return Set[x];
}
void UNION(int a,int b)
{
int a1=find(a);
int a2=find(b);
if(a1!=a2)
{
ANS[a1]+=ANS[a2];
Set[a2]=a1;
}
}
void input()
{
char ch;int temp;
CSH();
cin>>N;
getchar();
for(int i=1;i<=N;i++)
{
scanf("%c",&ch);
if(ch=='P')
{
tot++;
scanf("%lf%lf%lf%lf\n",&S[tot].x,&S[tot].y,&E[tot].x,&E[tot].y);
for(int i=1;i<tot;i++)
{
if(pan(S[i],E[i],S[tot],E[tot])==1)
{
UNION(i,tot);
}
}
}
else if(ch=='Q')
{
scanf("%d\n",&temp);
cout<<ANS[find(temp)]<<endl;
}
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int main()
{
int T;
// init();
cin>>T;
int nn=0;
while(T--)
{
if(nn++) cout<<endl;
input();
}
return 0;
}

【计算几何初步-线段相交+并查集】【HDU1558】Segment set的更多相关文章

  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. 判断线段相交(hdu1558 Segment set 线段相交+并查集)

    先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...

  4. HDU 1558 Segment set( 判断线段相交 + 并查集 )

    链接:传送门 题意:输入一个数 n 代表有 n 组操作,P 是在平面内加入一条线段,Q x 是查询第 x 条线段所在相交集合的线段个数 例如:下图 5 与 1.2 相交,1 与 3 相交,2 与 4 ...

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

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

  6. [poj 1127]Jack Straws[线段相交][并查集]

    题意: 给出一系列线段,判断某两个线段是否连通. 思路: 根据线段相交情况建立并查集, 在同一并查集中则连通. (第一反应是强连通分量...实际上只要判断共存即可, 具体的方向啊是没有关系的..) 并 ...

  7. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

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

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

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

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

随机推荐

  1. stm32之通用定时器TIM

    STM32系列的CPU,有多达8个定时器: 1.其中TMI1和TIM8是能够产生三对PWM互补输出的高级定时器,常用于三相电机的驱动:它们的时钟有APB2的输出产生: 2.其它6个为普通定时器,时钟由 ...

  2. EffectiveC#03--用委托表示回调,用事件定义对外接口

    1.回调的场景:我给了儿子一个任务且他可以报告状态来(重复的)打断我.而我在等待他完成任务的每一个部份时不用阻塞我自己的进程.他可以在有重要(或者事件)状态报告时,可以定时的打断我,或者向我询求帮助 ...

  3. 《JavaScript 闯关记》之单体内置对象

    ECMA-262 对内置对象的定义是「由 JavaScript 实现提供的.不依赖于宿主环境的对象,这些对象在 JavaScript 程序执行之前就已经存在了」.意思就是说,开发人员不必显式地实例化内 ...

  4. ie11加载不了css的问题

    ie11打开页面,竟然一点css都没有加载出来,而且是老大的电脑遇到了这个问题,啃爹啊 上网查资料,做了如下修改: 1.pc端最好不要用h5来写页面,兼容性是个头疼的问题,切记切记(移动端可以用H5来 ...

  5. box-shadow讲解1

    谈谈box-shadow的具体使用方法 语法: E {box-shadow: <length> <length> <length>?<length>?| ...

  6. hdu1166 经典线段入门

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. 我的开源框架之Accordion控件

    需求: (1)实现手风琴面板控件,支持静态HTML与JSON方式创建控件 (2)支持远程加载数据 (3)支持面板激活.远程加载事件注册 (4)支持动态添加.删除项目 实现图例 客户代码 <div ...

  8. 基于VMware的eCos应用程序测试(hello wold)

    (1)脚本配置ecosconfig new pccdl_component CYG_HAL_STARTUP { # Flavor: data # No user value, uncomment th ...

  9. python 在linux下通过top,和dh命令获得cpu,内存,以及硬盘信息

    主要是通过os.popen读取命令输出实现的,os.popen启动新的进程,且将外部命令的输出作为文件类型对象返回.不能获得外部命令的返回值.既然是文件对象就可以直接用for in 来读取,代码如下: ...

  10. python bottle框架(WEB开发、运维开发)教程

    教程目录 一:python基础(略,基础还是自己看书学吧) 二:bottle基础 python bottle web框架简介 python bottle 框架环境安装 python bottle 框架 ...