hdu1558——Segment set
来源:互联网 发布:超图软件 新浪财经 编辑:程序博客网 时间:2024/04/30 01:10
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
110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5
Sample Output
12225
Author
判断和某一条线段相交的线段有几条,基础并查集,关键是判断线段相交不要弄错
判断和某一条线段相交的线段有几条,基础并查集,关键是判断线段相交不要弄错
#include<stdio.h>#include<math.h>#include<algorithm>#include<iostream>using namespace std;struct node{double x1,x2,y1,y2;}line[1010];int father[1010];int rank[1010];int find(int x){if(x==father[x]) return father[x]; else father[x]=find(father[x]); return father[x];}void merge(int a,int b){int aa=find(a);int bb=find(b);if(aa!=bb){father[aa]=bb;rank[bb]+=rank[aa];} }bool judge(int a,int b){double min_ay=min(line[a].y1,line[a].y2);double max_ay=max(line[a].y1,line[a].y2);double min_by=min(line[b].y1,line[b].y2);double max_by=max(line[b].y1,line[b].y2);if(line[a].x1 == line[a].x2){if(line[b].x1 == line[b].x2){if(line[b].x1 != line[a].x1)//平行但不重合 return false;else{if(min_ay > line[b].y1 && min_ay > line[b].y2) return false; else if(max_ay < line[b].y1 && max_ay < line[b].y2) return false; else return true;} }else{double k2=(line[b].y1 - line[b].y2)/(line[b].x1 - line[b].x2);double b2=line[b].y1 - k2 * line[b].x1;double ly=k2 * line[a].x1 + b2;if(ly>=min_ay && ly <= max_ay && ly>=min_by && ly<=max_by) return true; else return false;}}else{if(line[b].x1 == line[b].x2){double k1=(line[a].y1 - line[a].y2)/(line[a].x1 - line[a].x2);double b1=line[a].y1 - k1 * line[a].x1;double ly=k1 * line[b].x1 + b1;if(ly>=min_ay && ly <= max_ay && ly>=min_by && ly<=max_by) return true; else return false;}else//两条直线都有斜率 {double k1=(line[a].y1 - line[a].y2)/(line[a].x1 - line[a].x2);double b1=line[a].y1 - k1 * line[a].x1;double k2=(line[b].y1 - line[b].y2)/(line[b].x1 - line[b].x2);double b2=line[b].y1 - k2 * line[b].x1;if(k1 == k2){if(b1 == b2){if(min_ay > line[b].y1 && min_ay > line[b].y2) return false; else if(max_ay < line[b].y1 && max_ay < line[b].y2) return false; else return true;} else return false;}else{double ly=((b2-b1)/(k1-k2))*k1+b1;if(ly>=min_ay && ly <= max_ay && ly>=min_by && ly<=max_by) return true; else return false;}}}}int main(){int t;scanf("%d",&t);bool flag=false;while(t--){if(flag)printf("\n");elseflag=true;int n;scanf("%d",&n);for(int i=0;i<=n;i++){rank[i]=1;father[i]=i;}char c[3];int cnt=1,res;double x1,y1,x2,y2;for(int i=0;i<n;i++){scanf("%s",c);if(c[0]=='P'){scanf("%lf%lf%lf%lf",&line[cnt].x1,&line[cnt].y1,&line[cnt].x2,&line[cnt].y2);for(int i=1;i<cnt;i++){if(judge(i,cnt))merge(i,cnt);}cnt++;}else{scanf("%d",&res);getchar();printf("%d\n",rank[find(res)]);}}}return 0;}
0 0
- hdu1558——Segment set
- ACM-计算几何之Segment set——hdu1558
- hdu1558 Segment set
- hdu1558 Segment set
- hdu1558 Segment set
- Segment set(HDU1558)(C)
- HDU1558 Segment set(计算几何+并查集)
- Segment set(hdu1558并查集+计算机和)
- hdu1558 Segment set(线段是否有交点+并查集)
- HDU1558 - Segment set 并查集 + 判断线段相交
- hdu1558 Segment set (判断线段相交+并查集)
- 【计算几何初步-线段相交+并查集】【HDU1558】Segment set
- hdu1558
- Segment set
- Segment set
- HDOJ 1558 Segment set
- HDU 1558 Segment Set
- hdu 1558 Segment set
- 关于Arcgis数据导入mdb完成后如何立刻解除mdb的锁定
- 【索引】String Algorithms::Examples
- 个人专著《企业级GIS设计管理优化策略》已经出版等待您的选购
- 基于Storm的Nginx log实时监控系统
- .net 验证码
- hdu1558——Segment set
- c语言使用中的一点感慨
- leetcode: N-Queues
- HOJ 2634 How to earn more [网络流]最大权闭合图
- OSX10.9 QT5.3.1 关于加载动态库(Cannot load library.....)
- 文章列表上一章
- 周期串
- 常用函数之指定目录下查询文件
- 文章列表下一章