64,688
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
double max ( double x1, double x2 ) { return x1>x2?x1:x2; }
double min ( double x1, double x2 ) { return x1<x2?x1:x2; }
struct Point
{
double x, y;
};
struct Segment
{
Point s, e;
}seg[1002];
int parent[1002];//父结点
double xmult ( Point s, Point a, Point b )
{
return ( a.x - s.x ) * ( b.y - s.y ) - ( b.x - s.x ) * ( a.y - s.y );
}
bool isIntersected(Point s1, Point e1, Point s2, Point e2)
{
if(
(max(s1.x, e1.x) >= min(s2.x, e2.x)) &&
(max(s2.x, e2.x) >= min(s1.x, e1.x)) &&
(max(s1.y, e1.y) >= min(s2.y, e2.y)) &&
(max(s2.y, e2.y) >= min(s1.y, e1.y)) &&
(xmult(s2, e1, s1) * xmult(e1, e2, s1) >= 0) &&
(xmult(s1, e2, s2) * xmult(e2, e1, s2) >= 0)
)
return true;
return false;
}
void UFset( )
{
int i;
for( i=1; i<=1001; i++ )
parent[i] = -1;
}
int Find( int x )
{
int s;
for( s=x; parent[s]>=0; s=parent[s] )
;
while( s!=x )
{
int tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union( int R1, int R2 )
{
int r1 = Find(R1), r2 = Find(R2);
int tmp = parent[r1] + parent[r2];
if( parent[r1] > parent[r2] )
{
parent[r1] = r2;
parent[r2] = tmp;
}
else
{
parent[r2] = r1;
parent[r1] = tmp;
}
}
int main()
{
int test, n, i, j, id;
char order;
cin>>test;
while(test--)
{
i=1;
cin>>n;
UFset();
while(n--)
{
cin>>order;
if(order=='P')
{
cin>>seg[i].s.x>>seg[i].s.y>>seg[i].e.x>>seg[i].e.y;
for(j=i-1; j>=1; j--)
if(isIntersected(seg[j].s, seg[j].e, seg[i].s, seg[i].e))
Union(j, i);
i++;
}
else
{
cin>>id;
cout<<-parent[Find(id)]<<endl;
}
}
if(test)
cout<<endl;
}
return 0;
}