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. 如何使用maven 打包源代码呢?

    如何使用maven 打包源代码呢? http://hw1287789687.iteye.com/blog/1943157

  2. php函数symlink详解

    php函数symlink详解 建立符号链接 (symbolic link),类似于Windows里头常用的.lnk快捷方式 symlink语法: int symlink(string target, ...

  3. windows.location属性(常用知识点)

    location对象 含有当前URL的信息. 属性 href 整个URL字符串.protocol 含有URL第一部分的字符串,如http:host 包含有URL中主机名:端口号部分的字符串.如//ww ...

  4. vue 键盘回车事件导致页面刷新的问题,路由多了一个问号

    问题: <el-form @submit.native.prevent> <el-form-item > <el-input @keyup.enter.native=&q ...

  5. Maven学习总结(15)——Maven 项目中pom.xml详解

    <project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2 ...

  6. windows下配置mycat与常见问题解决

    mycat官网:http://www.mycat.org.cn/ wiki:https://github.com/MyCATApache/Mycat-Server/wiki MyCat使用Mysql的 ...

  7. JDBC 专题

    digest: getFetchSize()方法不是获得记录数,而是获得每次抓取的记录数,默认是0,也就是说不限制.可以用setFetchSize()来设置,而getFetchSize()是用来读出那 ...

  8. datagridview合并相同单元格

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { // 对第日 ...

  9. Centos NFS 简单设置

    Server 端: NFS的安装配置:centos 5 :yum install nfs-utils portmapcentos 6 :yum install nfs-utils rpcbind vi ...

  10. 程序员的困境 - R中国用户组-炼数成金

    原文:http://www.oschina.net/news/43389/the-plight-of-programmer 在大型公司中不能腐蚀自己的学习能力和时间能力. 最近我为一个内核程序员的职位 ...