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
判断和某一条线段相交的线段有几条,基础并查集,关键是判断线段相交不要弄错
判断和某一条线段相交的线段有几条,基础并查集,关键是判断线段相交不要弄错
#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");
else
flag=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;
}