hdu 1558 Segment set (并查集)
2024-08-31 20:07:53
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.
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;
}
最新文章
- http协议(十一)http与https
- 原创:CSS3技术-雪碧图自适应缩放与精灵动画方案
- 封装WCF客户端调用
- php模式设计之 适配器模式
- 常见的Mule Esb下载地址
- 黄聪:WebBrowser执行和安装jQuery脚本(IEBrowse)
- 李洪强iOS开发之自定义cell的使用
- 我所理解的OOP——UML六种关系(转)
- 兼容IE的渐变
- 用 C 语言编写 Windows 服务程序的五个步骤
- JAVA关键字transient
- 学习ORM框架—hibernate(三):跟踪持久化对象状态,掌握对象持久化
- (转)浅析CSS——元素重叠及position定位的z-index顺序
- SqlSugar ORM 入门到精通【一】入门篇
- MyBatis源码解析(九)——Type类型模块之类型处理器注册器(TypeHandlerRegistry)
- 【Json】1、JSON 数据格式
- python数据分析Adult-Salary预测
- 构建你的长寿命的API第1部分:规范驱动的API开发
- eterm和easyfare的官网地址
- Matlab学以致用 - 曲线拟合
热门文章
- Android系统如何管理自己内存的?
- 用实力让情怀落地!阅兵前线指挥车同款电视TCL&;#160;H8800受捧
- [Android 性能优化系列]内存之提升篇--应用应该怎样管理内存
- 目标识别(object detection)中的 IoU(Intersection over Union)
- Android 自己定义主菜单
- Hibernate与代理模式
- Android 系统状态栏一体化实现
- Linux中U盘和SD卡加载卸载命令
- 将OpenCV捕获的摄像头加载到picture控件中
- [Angular2 Form] Model Driven Form Custom Validator