Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3102 Accepted Submission(s): 1166
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
题意:P 给出一条线段的坐标
Q 求出与这条线段直接与间接相交的线段的数量
思路:赤裸裸的并查集+计算几何(两线段相交)
理解两线段相交的叉乘方法花了很久,证明方法详见<算法合集-计算几何>
CODE:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
struct sheng
{
double x1;double y1;
double x2;double y2;
}sh[1005];
int set[1005];
int num[1005];
void init(int nn)
{
for(int i=1;i<=nn;i++)
{
set[i]=i;
num[i]=1;
}
}
int find(int a)
{
if(a==set[a]) return a;
else return set[a]=find(set[a]);
}
void unite(int a,int b)
{
int fa=find(a),fb=find(b);
if(fa!=fb)
{
set[fb]=fa;
num[fa]+=num[fb];
}
else return ;
}
double chaji(double a1,double b1,double a2,double b2)
{
return a1*b2-a2*b1;
}
bool xiangjiao(int i,int k)
{
if(max(sh[k].x1,sh[k].x2)>=min(sh[i].x1,sh[i].x2)
&& min(sh[k].x1,sh[k].x2)<=max(sh[i].x1,sh[i].x2)
&& min(sh[k].y1,sh[k].y2)<=max(sh[i].y1,sh[i].y2)
&& max(sh[k].y1,sh[k].y2)>=min(sh[i].y1,sh[i].y2))
{
double t1=chaji(sh[k].x2-sh[k].x1,sh[k].y2-sh[k].y1,sh[i].x2-sh[k].x1,sh[i].y2-sh[k].y1);
double t2=chaji(sh[k].x2-sh[k].x1,sh[k].y2-sh[k].y1,sh[i].x1-sh[k].x1,sh[i].y1-sh[k].y1);
double t3=chaji(sh[i].x1-sh[i].x2,sh[i].y1-sh[i].y2,sh[k].x1-sh[i].x2,sh[k].y1-sh[i].y2);
double t4=chaji(sh[i].x1-sh[i].x2,sh[i].y1-sh[i].y2,sh[k].x2-sh[i].x2,sh[k].y2-sh[i].y2);
if(t1*t2<=0&&t3*t4<=0) return true;
}
return false;
}
int main()
{
//freopen("poj1558.txt","r",stdin);
int t,n;
scanf("%d",&t);
//getchar();
while(t--)
{
int k=1;
scanf("%d",&n);
//getchar();
init(n);
while(n--)
{
char ch[2];
scanf("%s",ch);
if(ch[0]=='P')
{
scanf("%lf%lf%lf%lf",&sh[k].x1,&sh[k].y1,&sh[k].x2,&sh[k].y2);
for(int i=1;i<k;i++)
{
if(xiangjiao(i,k))
unite(i,k);
}
k++;
//getchar();
}
if(ch[0]=='Q')
{
int que;
scanf("%d",&que);
printf("%d\n",num[find(que)]);
//getchar();
}
}
if(t!=0) printf("\n");
}
return 0;
}