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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值