Segment set(普通并查集+计算几何两线段相交与否)

来源:互联网 发布:手机淘宝联盟能推广吗 编辑:程序博客网 时间:2024/06/05 03:25

hdu1558 判断两线段相不相交贴的模板,暂时没搞懂

#include <iostream>#include <stdio.h>#include <math.h>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <string>#include <string.h>#include <map>#include <set>using namespace std;int f[1001];typedef struct point{    double x;    double y;}Point;//判断直线AB是否与线段CD相交bool lineIntersectSide(Point A, Point B, Point C, Point D){    // A(x1, y1), B(x2, y2)的直线方程为:    // f(x, y) =  (y - y1) * (x1 - x2) - (x - x1) * (y1 - y2) = 0    double fC = (C.y - A.y) * (A.x - B.x) - (C.x - A.x) * (A.y - B.y);    double fD = (D.y - A.y) * (A.x - B.x) - (D.x - A.x) * (A.y - B.y);    if(fC * fD > 0)        return false;    return true;}bool sideIntersectSide(Point A, Point B, Point C, Point D){    if(!lineIntersectSide(A, B, C, D))        return false;    if(!lineIntersectSide(C, D, A, B))        return false;    return true;}struct line{    double x1,y1,x2,y2;};line l[1005];int findfa(int x){    if(x==f[x])    {        return x;    }    else    {         return  f[x]=findfa(f[x]);    }}int main(){   int t,i,m,k,id,ans;   char s[2];   double x1,y1,x2,y2;   scanf("%d",&t);   int o=1;   while(t--)   {       if(o!=1)       printf("\n");       scanf("%d",&m);       for(i=1;i<=1000;i++)        f[i]=i;       id=1;       while(m--)       {           scanf("%s",s);           if(s[0]=='P')           {               scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);               l[id].x1=x1;l[id].x2=x2;l[id].y1=y1;l[id].y2=y2;               for(i=1;i<=id-1;i++)               {                   point A={l[id].x1,l[id].y1};                   point B={l[id].x2,l[id].y2};                   point C={l[i].x1,l[i].y1};                   point D={l[i].x2,l[i].y2};                   if(sideIntersectSide(A,B,C,D))                   {                      // cout<<"id:"<<id<<" ";                       int tx=findfa(i),ty=findfa(id);                       if(tx!=ty)                        f[ty]=tx;                   }               }               id++;           }           else           {               scanf("%d",&k);               ans=0;               for(i=1;i<=id;i++)               {                 int t=findfa(i);               }               for(i=1;i<=id;i++)               {                   if(f[k]==f[i])ans++;               }               printf("%d\n",ans);           }       }       o++;   }    return 0;}


0 0