HDU_1558_SegmentSet
2015-08-11 10:54
344 查看
Segment set
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4100 Accepted Submission(s): 1550
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
Source
HDU 2006-12 Programming Contest
Recommend
LL
线段相交+并查集问题
问题问的是集合中的元素个数
因此合并操作的时候也要合并元素个数
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int MN=1005; const double AC=1e-3; char con[2]; double se[MN][4]; //输入的直线 int senc[MN]; //直线的几何关系 int sen[MN]; //集合中的直线个数 int find(int n) //查找 { if(senc ==n) return n; int t=senc ; return senc =find(t); } double dt(double v1,double v2,double v3,double v4) //行列式 { return v1*v4-v2*v3; } bool isx(int i,int j) //判断相交 { double t1=dt(se[i][2]-se[i][0],se[j][0]-se[j][2],se[i][3]-se[i][1],se[j][1]-se[j][3]); if(t1<=AC&&t1>=-AC) return 0; double t2=dt(se[j][0]-se[i][0],se[j][0]-se[j][2],se[j][1]-se[i][1],se[j][1]-se[j][3])/t1; if(t2>1||t2<0) return 0; double t3=dt(se[i][2]-se[i][0],se[j][0]-se[i][0],se[i][3]-se[i][1],se[j][1]-se[i][1])/t1; if(t3>1||t3<0) return 0; return 1; } void mer(int i,int j) { //cout<<i<<" "<<j<<endl; int t1=find(i); int t2=find(j); if(t1==t2) return; sen[t2]=sen[t1]+sen[t2]; //合并元素个数 senc[t1]=t2; //cout<<t2<<" "<<sen[t2]<<endl; } int main() { int t; int n; int nn; scanf("%d",&t); while(t--) { int p=1; scanf("%d",&n); for(int i=1;i<=n;i++) //重置 { senc[i]=i; sen[i]=1; } for(int i=1;i<=n;i++) { scanf("%s",con); if(con[0]=='Q') { scanf("%d",&nn); printf("%d\n",sen[find(nn)]); } else { scanf("%lf%lf%lf%lf",&se[p][0],&se[p][1],&se[p][2],&se[p][3]); for(int i=1;i<p;i++) { if(isx(i,p)) mer(i,p); } p++; } } if(t) printf("\n"); } return 0; }
相关文章推荐
- 算法竞赛入门经典: 第四章 函数与递归 4.1求两点之间距离
- 21-IO流-21-IO流(字节流-操作文件基本演示)
- makefile的语法及写法
- android 内存管理机制、异常、垃圾回收
- iOS 9 分屏多任务(2):Slide Over & Split View快速入门
- iOS开发:创建真机调试证书
- swift-01-简述swift与OC区别
- 修改tomcat浏览器地址栏图标
- swift-01-简述swift与OC区别
- 如何在Ubuntu 14.04中安装最新版Eclipse
- IOS开发笔记之常用的第三方库(太多了。)
- 编程小技巧
- mysql复合索引、普通索引总结
- Android Interface Definition Language (AIDL)
- String、StringBuffer与StringBuilder之间区别
- 用Sublime Text 2 作IDE开发 Unity3D 项目(VinceLi)
- mysqldump参数之数据一致性
- IOS视频播放器VKVideoPlayer
- 21-IO流-20-IO流(字符流-缓冲区-LineNumberReader)
- 白话设计模式之--单例模式