zoukankan      html  css  js  c++  java
  • hdu 1558 Segment set

    http://acm.hdu.edu.cn/showproblem.php?pid=1558

    先判断线段相交,然后用并查集合并。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 20000
     5 using namespace std;
     6 
     7 int num[maxn],f[maxn];
     8 
     9 struct point
    10 {
    11     double x,y;
    12 };
    13 struct line
    14 {
    15     point p1,p2;
    16 }p[maxn];
    17 
    18 
    19 void inti()
    20 {
    21     for(int i=0; i<=maxn; i++)
    22     {
    23         f[i]=i;
    24         num[i]=1;
    25     }
    26 }
    27 double det(point a,point b,point c)
    28 {
    29     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
    30 }
    31 
    32 int find1(int x)
    33 {
    34     if(x==f[x]) return x;
    35     return f[x]=find1(f[x]);
    36 }
    37 
    38 void merge1(int a,int b)
    39 {
    40     int fa=find1(a);
    41     int fb=find1(b);
    42     if(fa!=fb)
    43     {
    44         f[fa]=fb;
    45         num[fb]+=num[fa];
    46     }
    47 }
    48 
    49 bool Isintsert(line u,line v)
    50 {
    51     return (det(v.p1,u.p2,u.p1)*det(u.p2,v.p2,u.p1)>=0)&&(det(u.p1,v.p2,v.p1)*det(v.p2,u.p2,v.p1)>=0)&&
    52     (max(u.p1.x,u.p2.x)>=min(v.p1.x,v.p2.x))&&
    53     (max(v.p1.x,v.p2.x)>=min(u.p1.x,u.p2.x))&&
    54     (max(u.p1.y,u.p2.y)>=min(v.p1.y,v.p2.y))&&
    55     (max(v.p1.y,v.p2.y))>=min(u.p1.y,u.p2.y);
    56 }
    57 
    58 int main()
    59 {
    60     int t,n,k1;
    61     char ch;
    62     scanf("%d",&t);
    63     while(t--)
    64     {
    65         inti();
    66         scanf("%d",&n);
    67         getchar();
    68         int t1=1;
    69         for(int i=0; i<n; i++)
    70         {
    71             scanf("%c",&ch);
    72             if(ch=='P')
    73             {
    74                 scanf("%lf%lf%lf%lf",&p[t1].p1.x,&p[t1].p1.y,&p[t1].p2.x,&p[t1].p2.y);
    75                 t1++;
    76                 for(int j=1; j<t1-1; j++)
    77                 {
    78                     int f2=find1(t1-1); int f3=find1(j);
    79                     if(f2!=f3&&Isintsert(p[j],p[t1-1]))
    80                     {
    81                         merge1(j,t1-1);
    82                     }
    83                 }
    84             }
    85             else if(ch=='Q')
    86             {
    87                 scanf("%d",&k1);
    88                 int k2=find1(k1);
    89                 printf("%d
    ",num[k2]);
    90             }
    91             getchar();
    92             
    93         }
    94         if(t!=0) printf("
    ");
    95     }
    96     return 0;
    97 }
    View Code
  • 相关阅读:
    pthread条件变量
    c++信号处理
    必杀技
    待飞日记(第四天和第五天)
    c++面试题总结(2)
    比起主流的30秒,10秒广告能获得2倍的效果
    c++面试题总结(1)
    待飞日记(第三天)
    static_cast, dynamic_cast, const_cast探讨
    c++一些问题总结
  • 原文地址:https://www.cnblogs.com/fanminghui/p/3726438.html
Copyright © 2011-2022 走看看