hdu_1558_Segment set(并查集+计算几何)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1558
题意:P为画线段,Q为询问当前这条线段所在的集合有多少线段
题解:如果两条线段有交点,那么就连接这两个集合
1 #include<cstdio> 2 #define FFC(i,a,b) for(int i=a;i<=b;++i) 3 4 struct line{double x1,y1,x2,y2;}a[1010]; 5 int n,t,ans,ed,tp,now,f[1010];double eps=1e-10;char cmd[2]; 6 7 int is(line a,line b){ 8 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; 9 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; 10 return 1; 11 } 12 13 int find(int x){return f[x]==x?x:(f[x]=find(f[x]));} 14 void merge(int x,int y){int xx,yy;if((xx=find(x))!=(yy=find(y)))f[xx]=yy;} 15 16 int main(){ 17 scanf("%d",&t); 18 while(t--){ 19 scanf("%d",&n),ed=0; 20 FFC(i,1,n)f[i]=i; 21 FFC(i,1,n){ 22 scanf("%s",cmd); 23 if(cmd[0]=='P'){ 24 ed++,scanf("%lf%lf%lf%lf",&a[ed].x1,&a[ed].y1,&a[ed].x2,&a[ed].y2); 25 FFC(j,1,ed-1)if(is(a[j],a[ed]))merge(ed,j); 26 }else { 27 scanf("%d",&tp),ans=0,now=find(tp); 28 FFC(i,1,ed)if(find(i)==now)ans++; 29 printf("%d\n",ans); 30 } 31 } 32 if(t)puts(""); 33 } 34 return 0; 35 }