Segment set

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

Total Submission(s): 3907    Accepted Submission(s): 1471

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
 

题目大意:在一个平面直角坐标系里面,通过P操作不断的增加线段,假设两个线段有相交。就表明他们是一个集合里面的。Q操作询问当前情况下第k条线段所在的集合里面有几条线段。

并查集的题目,可是我认为主要考几何。我開始能够想到。通过推断两条线段是否有交点,假设有就放在一个集合里面。这么想的确非常easy,可是做起来真的十分麻烦。。

假设对于两条线段,能够通过简单计算得到两者的交点x0=(b2-b1)/(k1-k2),还有y0。

那么我仅仅要推断x0,y0是否在线段相交的地方就可以。可是还要注意,这个交点是从k1,k2得到的。所以假设k1,k2不存在,又要分情况讨论。

下面是我的代码,感觉好像还有遗漏的地方,尽管的确是AC了。

#include<stdio.h>
#include<string.h>
int p[10000],sum[10000];
double x1[1005],x2[1005],y1[1005],y2[1005];
void init(int x)
{
int i;
for(i=0;i<=x;i++)
p[i]=i;
for(i=0;i<=x;i++)
sum[i]=1;
}
int findroot(int x)
{
int r=x;
while(r!=p[r])
r=p[r];
int i,j;
i=x;
while(i!=r)
{
j=p[i];
p[i]=r;
i=j;
}
return r;
}
void merge(int x,int y)
{
int fx=findroot(x);
int fy=findroot(y);
if(fx!=fy){
p[fx]=fy;
sum[fy]+=sum[fx];
}
}
double jiaodian(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{ if(x1==x2&&x3!=x4){ //k1不存在,k2存在
double k2=(y3-y4)/(x3-x4);
double y=k2*(x1-x3)+y3;
if((y>=y1&&y<=y2)||(y>=y2&&y<=y1))return 1;
else return 0;
}
else if(x3==x4&&x1!=x2){ //k2不存在,k1存在
double k1=(y1-y2)/(x1-x2);
double y=k1*(x3-x1)+y1;
if((y>=y3&&y<=y4)||(y>=y4&&y<=y3))return 1;
else return 0;
}
else if(x1==x2&&x3==x4){
if(x1==x3&&((y1>=y3&&y1<=y4)||(y1>=y4&&y1<=y3)||(y2>=y4&&y2<=y3)||(y2>=y3&&y2<=y4)))return 1;
else return 0;
}
double k1=(y1-y2)/(x1-x2);
double k2=(y3-y4)/(x3-x4);
double b1=(x1*y2-x2*y1)/(x1-x2);
double b2=(x3*y4-x4*y3)/(x3-x4);
double x=(b2-b1)/(k1-k2);
double y=k1*(x-x1)+y1;
if(((x>=x1&&x<=x2)||(x>=x2&&x<=x1))&&((y>=y1&&y<=y2)||(y>=y2&&y<=y1))&&
((x>=x3&&x<=x4)||(x>=x4&&x<=x3))||((y>=y3&&y<=y4)&&(y>=y4&&y<=y3)))return 1;
return 0;
}
void isconnect(int x)
{
int i;
for(i=1;i<=x;i++)
{ if(jiaodian(x1[i],y1[i],x2[i],y2[i],x1[x],y1[x],x2[x],y2[x])){merge(i,x);}
}
return ;
}
int main()
{
int t,n,i,j,k,m,cnt,q; char c[10];
scanf("%d",&t);
while(t--)
{
q=1;
scanf("%d",&n);
init(n); cnt=1;
for(i=1;i<=n;i++)
{
scanf("%s",c);
if(c[0]=='P')
{
scanf("%lf%lf%lf%lf",&x1[q],&y1[q],&x2[q],&y2[q]); if(i>1){
isconnect(q); }
q++;
}
if(c[0]=='Q'){
scanf("%d",&k);
int s=findroot(k);
cnt=sum[s];
printf("%d\n",cnt); } }
if(t>0)printf("\n");
}
return 0;
}

hdu 1558 Segment set (并查集)的更多相关文章

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

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

  2. hdu 1558 线段相交+并查集路径压缩

    Segment set Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

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

  4. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  5. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

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

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

  7. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

  8. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

  9. HDU 4313 Matrix(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...

随机推荐

  1. 【Codeforces Round #453 (Div. 2) A】 Visiting a Friend

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 维护最右端的端点就好. [代码] #include <bits/stdc++.h> using namespace st ...

  2. 用电脑从Google Play下载apk

    用电脑从Google Play下载apk 方法一:给Chrome浏览器安装apk-downloader插件,需禁止 SSL 错误警告,即在Chrome的快捷方式上加入"--ignore-ce ...

  3. Linux下读写寄存器

    arm裸机下读写寄存器很容易,各个寄存器和内存的地址是单一地址空间,他们是用相同的指令进行读写操作的.而在linux下就要复杂很多,因为linux支持多个体系架构的CPU.比如arm和x86就不一样, ...

  4. C#调用第三方ocx控件 (winform /aspx)

    C#调用第三方ocx控件   1..net环境在工具箱上点右键,选择自定义工具箱,然后选择你需要的COM或者OCX控件就可以了. 2.在自定义工具箱中加入相应的控件,设置id,在客户端脚本中直接引用它 ...

  5. 【 Codeforces Round #430 (Div. 2) A 】 Kirill And The Game

    [链接]点击打开链接 [题意] 水题 [题解] 枚举b从x..y看看k*i是不是在l..r之间就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <cstdio> ...

  6. linux删除svn版本号库

    当使用了svn版本号控制系统后每一个文件夹下都会有一个.svn文件夹存在,开发完当交付产品或者上传到server时一般要把这些文件夹删除.事实上在linux删除这些文件夹是非常easy的,命令例如以下 ...

  7. 三星Galaxy Tab S2上市,压制苹果之心凸显

        平板市场正在迎来史上最为关键的一次PK,众所周知,平板市场的苹果和三星一直是行业的领头羊,而在激烈的竞争中.三星平板似乎后劲更足.众多性能优异的产品频频推出.平板之王的称谓呼之欲出. 去年三星 ...

  8. 安装filezilla client报错libgnutls版本旧

    http://blog.csdn.net/mofabang/article/details/9212217

  9. [Angular2 Router] Redirects and Path Matching - Avoid Common Routing Pitfall

    In this tutorial we are going to learn how we can can configure redirects in the angular 2 router co ...

  10. 【习题 3-6 UVA - 232】Crossword Answers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟题.注意场宽为3 [代码] #include <bits/stdc++.h> using namespace std ...