HDU 1558 计算几何+并查集
Segment set
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2896 Accepted Submission(s): 1073
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
线段相交判断+并查集。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const double eps = 1e-8;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
}pp[1010];
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
int fa[1010],num[1010];
int find(int x)
{
if(fa[x]!=x)fa[x]=find(fa[x]);
return fa[x];
}
void uni(int x,int y)
{
if(x==y)return;
if(inter(pp[x],pp[y]))
{
int fx=find(x);
int fy=find(y);
if(fx==fy)return;
fa[fx]=fy;
// num[fx]+=num[fy];
num[fy]+=num[fx];
}
}
int n,m;
int main()
{
int i,j,k,T;char ch[30];
scanf("%d",&T);
while(T--)
{
n=0;
scanf("%d",&m);memset(num,0,sizeof(num));
while(m--)
{
scanf("%s",ch);
if(ch[0]=='P')
{
Point p,q;
scanf("%lf%lf%lf%lf",&p.x,&p.y,&q.x,&q.y);
pp[++n]=Line(p,q);
fa[n]=n;num[n]++;
for(i=1;i<n;i++)uni(i,n);
}
else
{
scanf("%d",&i);
int fx=find(i);
printf("%d\n",num[fx]);
}
}
if(T)puts("");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const double eps = 1e-8;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0)return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
}pp[1010];
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
int fa[1010],num[1010];
int find(int x)
{
if(fa[x]!=x)fa[x]=find(fa[x]);
return fa[x];
}
void uni(int x,int y)
{
if(x==y)return;
if(inter(pp[x],pp[y]))
{
int fx=find(x);
int fy=find(y);
if(fx==fy)return;
fa[fx]=fy;
// num[fx]+=num[fy];
num[fy]+=num[fx];
}
}
int n,m;
int main()
{
int i,j,k,T;char ch[30];
scanf("%d",&T);
while(T--)
{
n=0;
scanf("%d",&m);memset(num,0,sizeof(num));
while(m--)
{
scanf("%s",ch);
if(ch[0]=='P')
{
Point p,q;
scanf("%lf%lf%lf%lf",&p.x,&p.y,&q.x,&q.y);
pp[++n]=Line(p,q);
fa[n]=n;num[n]++;
for(i=1;i<n;i++)uni(i,n);
}
else
{
scanf("%d",&i);
int fx=find(i);
printf("%d\n",num[fx]);
}
}
if(T)puts("");
}
return 0;
}

浙公网安备 33010602011771号