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. 计科1111-1114班第一次实验作业(NPC问题——回溯算法、聚类分析)

    实验课安排 地点: 科技楼423 时间:  计科3-4班---15周周一上午.周二下午 计科1-2班---15周周一下午.周二晚上(晚上时间从18:30-21:10) 请各班学委在实验课前飞信通知大家 ...

  2. IIS7性能优化

    http://www.03389.com/BLOG/user1/qq66565841/archives/2014/2014112143553.html IIS7性能优化 IIS7 优化-网站请求并发数 ...

  3. Flume Sinks官网剖析(博主推荐)

    不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) Flume Channel Selectors官网剖析(博主推荐) 一切来源于f ...

  4. metabase实施文档

    安装提前:需要安装JDK1.8以上 软件下载地址: https://metabase.com 还需要下载 ojdbc7.jar,以支持Oracle驱动 下载地址:http://www.oracle.c ...

  5. (入门整理学习一)Asp.net core

    1.安装.net code SDK,vs Code;vs code c#插件可在软件扩展 (我网盘有)  vs2015上安装教程:http://www.cnblogs.com/wangrudong00 ...

  6. ASM学习笔记--ASM 4 user guide 第二章要点翻译总结

    参考:ASM 4 user guide 第一部分 core API 第二章  类 2.1.1概观 编译后的类包括: l  一个描述部分:包括修饰语(比如public或private).名字.父类.接口 ...

  7. 2017.1-TOP5 Android开源库

    Colorful (Github) Colorful简单实用,通过这个开源库可以通过编码的方式来改变应用的主题,不再需要定义不同的style dependencies { compile 'com.g ...

  8. Android 网络图片Url 转 Bitmap

    注意:该方法必须要在子线程中调用,因为涉及网络请求 public Bitmap getBitmap(String url) { Bitmap bm = null; try { URL iconUrl ...

  9. Fragment Summary 2/2

    出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API ...

  10. 三个Bootstrap免费字体和图标库

    前言:Bootstrap 简洁.直观.强悍.移动设备优先的前端开发框架,让web开发更迅速.简单 ,深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快, 组件无数 ...