题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1558
题意:P为画线段,Q为询问当前这条线段所在的集合有多少线段
题解:如果两条线段有交点,那么就连接这两个集合
#include<cstdio>
#define FFC(i,a,b) for(int i=a;i<=b;++i)
struct line{double x1,y1,x2,y2;}a[1010];
int n,t,ans,ed,tp,now,f[1010];double eps=1e-10;char cmd[2];
int is(line a,line b){
if(((a.x2-a.x1)*(b.y1-a.y1)-(b.x1-a.x1)*(a.y2-a.y1))*((a.x2-a.x1)*(b.y2-a.y1)-(b.x2-a.x1)*(a.y2-a.y1))>eps)return 0;
if(((b.x2-b.x1)*(a.y1-b.y1)-(a.x1-b.x1)*(b.y2-b.y1))*((b.x2-b.x1)*(a.y2-b.y1)-(a.x2-b.x1)*(b.y2-b.y1))>eps)return 0;
return 1;
}
int find(int x){return f[x]==x?x:(f[x]=find(f[x]));}
void merge(int x,int y){int xx,yy;if((xx=find(x))!=(yy=find(y)))f[xx]=yy;}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n),ed=0;
FFC(i,1,n)f[i]=i;
FFC(i,1,n){
scanf("%s",cmd);
if(cmd[0]=='P'){
ed++,scanf("%lf%lf%lf%lf",&a[ed].x1,&a[ed].y1,&a[ed].x2,&a[ed].y2);
FFC(j,1,ed-1)if(is(a[j],a[ed]))merge(ed,j);
}else {
scanf("%d",&tp),ans=0,now=find(tp);
FFC(i,1,ed)if(find(i)==now)ans++;
printf("%d\n",ans);
}
}
if(t)puts("");
}
return 0;
}