ACM-计算几何之Segment set——hdu1558
2014-04-21 19:21
260 查看
Segment set
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558
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
这道题题意就是:
给你N个命令,如果命令为P,则向平面内添加一条线段,并判断是否与之前的相交,若相交则并入相应集合内。
如果命令为Q ,则输出线段号为Q后面数字 所在集合的线段总数。
解题:
首先肯定要用到线段相交。
其次,如何判断集合内的个数?————并查集!
恩,用并查集如何取该集合个数呢?
从每个集合所唯一的根下手,让根来存储集合内元素个数。
因为无论是合并,还是查找某元素,最终都会追溯到该集合的根。
每次合并的时候,主集合将自己和附属集合的个数和存在主集合内就可以。
不过这要开两个数组来完成啦~一个集合作为father数组,正常的并查集,另一个用来存储集合元素个数。
初始化的时候要使每个元素个数都为1(就是自己)。
PS:格式!!又WA一次。。。o(╯□╰)o啊~~~
每两组数据间有一个空行,最后一个数据后没有空行~!
#include <iostream>
#include <string.h>
using namespace std;
const double EPS = 1e-10;
#define MAX 1001
struct point
{
double x,y;
};
struct line
{
point a,b;
}l[MAX];
int father[MAX],num[MAX];
double Max(double a,double b) {return a>b?a:b;}
double Min(double a,double b) {return a>b?b:a;}
// 判断两线段是否相交(非规范相交)
bool inter(line l1,line l2)
{
point p1,p2,p3,p4;
p1=l1.a;p2=l1.b;
p3=l2.a;p4=l2.b;
if( Min(p1.x,p2.x)>Max(p3.x,p4.x) ||
Min(p1.y,p2.y)>Max(p3.y,p4.y) ||
Min(p3.x,p4.x)>Max(p1.x,p2.x) ||
Min(p3.y,p4.y)>Max(p1.y,p2.y) )
return 0;
double k1,k2,k3,k4;
k1 = (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y)*(p3.x-p1.x);
k2 = (p2.x-p1.x)*(p4.y-p1.y) - (p2.y-p1.y)*(p4.x-p1.x);
k3 = (p4.x-p3.x)*(p1.y-p3.y) - (p4.y-p3.y)*(p1.x-p3.x);
k4 = (p4.x-p3.x)*(p2.y-p3.y) - (p4.y-p3.y)*(p2.x-p3.x);
return (k1*k2<=EPS && k3*k4<=EPS);
}
//初始化函数
void Init(int n)
{
int i;
for(i=1;i<=n;i++)
{
father[i]=i;
num[i]=1;
}
}
//查找函数
int Find(int x)
{
while(father[x]!=x)
x=father[x];
return x;
}
//合并函数
void combine(int a,int b)
{
int temp_a,temp_b;
temp_a=Find(a);
temp_b=Find(b);
// 在合并集合的时候,相应集合所含的个数也要合并
if(temp_a!=temp_b)
{
father[temp_a]=temp_b;
num[temp_b]+=num[temp_a];
}
}
int main()
{
int test,i,n,k,js;
char c;
cin>>test;
while(test--)
{
js=0;
cin>>n;
Init(n);
while(n--)
{
cin>>c;
// 判断是P还是Q
if(c=='P')
{
++js;
cin>>l[js].a.x>>l[js].a.y>>l[js].b.x>>l[js].b.y;
// 判断该线段与之前线段是否相交,相交则合并
for(i=1;i<js;++i)
{
if( inter(l[js],l[i]) )
combine(js,i);
}
}
else
{
cin>>k;
cout<<num[Find(k)]<<endl;
}
}
// 格式!!!很重要,最后一组测试数据后无空行
if(test) cout<<endl;
}
return 0;
}
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558
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
这道题题意就是:
给你N个命令,如果命令为P,则向平面内添加一条线段,并判断是否与之前的相交,若相交则并入相应集合内。
如果命令为Q ,则输出线段号为Q后面数字 所在集合的线段总数。
解题:
首先肯定要用到线段相交。
其次,如何判断集合内的个数?————并查集!
恩,用并查集如何取该集合个数呢?
从每个集合所唯一的根下手,让根来存储集合内元素个数。
因为无论是合并,还是查找某元素,最终都会追溯到该集合的根。
每次合并的时候,主集合将自己和附属集合的个数和存在主集合内就可以。
不过这要开两个数组来完成啦~一个集合作为father数组,正常的并查集,另一个用来存储集合元素个数。
初始化的时候要使每个元素个数都为1(就是自己)。
PS:格式!!又WA一次。。。o(╯□╰)o啊~~~
每两组数据间有一个空行,最后一个数据后没有空行~!
#include <iostream>
#include <string.h>
using namespace std;
const double EPS = 1e-10;
#define MAX 1001
struct point
{
double x,y;
};
struct line
{
point a,b;
}l[MAX];
int father[MAX],num[MAX];
double Max(double a,double b) {return a>b?a:b;}
double Min(double a,double b) {return a>b?b:a;}
// 判断两线段是否相交(非规范相交)
bool inter(line l1,line l2)
{
point p1,p2,p3,p4;
p1=l1.a;p2=l1.b;
p3=l2.a;p4=l2.b;
if( Min(p1.x,p2.x)>Max(p3.x,p4.x) ||
Min(p1.y,p2.y)>Max(p3.y,p4.y) ||
Min(p3.x,p4.x)>Max(p1.x,p2.x) ||
Min(p3.y,p4.y)>Max(p1.y,p2.y) )
return 0;
double k1,k2,k3,k4;
k1 = (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y)*(p3.x-p1.x);
k2 = (p2.x-p1.x)*(p4.y-p1.y) - (p2.y-p1.y)*(p4.x-p1.x);
k3 = (p4.x-p3.x)*(p1.y-p3.y) - (p4.y-p3.y)*(p1.x-p3.x);
k4 = (p4.x-p3.x)*(p2.y-p3.y) - (p4.y-p3.y)*(p2.x-p3.x);
return (k1*k2<=EPS && k3*k4<=EPS);
}
//初始化函数
void Init(int n)
{
int i;
for(i=1;i<=n;i++)
{
father[i]=i;
num[i]=1;
}
}
//查找函数
int Find(int x)
{
while(father[x]!=x)
x=father[x];
return x;
}
//合并函数
void combine(int a,int b)
{
int temp_a,temp_b;
temp_a=Find(a);
temp_b=Find(b);
// 在合并集合的时候,相应集合所含的个数也要合并
if(temp_a!=temp_b)
{
father[temp_a]=temp_b;
num[temp_b]+=num[temp_a];
}
}
int main()
{
int test,i,n,k,js;
char c;
cin>>test;
while(test--)
{
js=0;
cin>>n;
Init(n);
while(n--)
{
cin>>c;
// 判断是P还是Q
if(c=='P')
{
++js;
cin>>l[js].a.x>>l[js].a.y>>l[js].b.x>>l[js].b.y;
// 判断该线段与之前线段是否相交,相交则合并
for(i=1;i<js;++i)
{
if( inter(l[js],l[i]) )
combine(js,i);
}
}
else
{
cin>>k;
cout<<num[Find(k)]<<endl;
}
}
// 格式!!!很重要,最后一组测试数据后无空行
if(test) cout<<endl;
}
return 0;
}
相关文章推荐
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification 线性变换||计算几何
- HDU 4998 Rotate 计算几何 2014 ACM/ICPC Asia Regional Anshan Online
- 【转载】ACM计算几何题目推荐
- ACM-计算几何 #Topcoder #SRM187-DIV2 PointInPolygon
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 I GSM Base Station Identification(暴力,计算几何)
- Myacm Triangles hoj,poj,uva 计算几何
- ACM计算几何题目推荐(1)
- HDU 5572 An Easy Physics Problem(计算几何)——2015ACM/ICPC亚洲区上海站-重现赛
- 山东省第四届ACM大学生程序设计竞赛-Rescue The Princess(计算几何)
- 【转载】ACM计算几何题目推荐
- ACM--计算几何--FZU--2110--Star
- ACM-计算几何之Lifting the Stone——hdu1115
- ACM计算几何题目推荐【转】
- ACM计算几何题目推荐(2)
- ACM计算几何题目推荐
- acm 计算几何题目集合
- ACM 计算几何模板 点在三角形内 加强版 HDU4380
- ACM-计算几何之改革春风吹满地——hdu2036
- HDU5572(2015ACM/ICPC亚洲区上海站A题)_计算几何(积累计算几何的板子)
- ACM-计算几何之Scrambled Polygon——poj2007