并查集 HDU1558

  1 #include <iostream>
  2 #include <set>
  3 #include <cstring>
  4 #include <cstdio>
  5 
  6 using namespace std;
  7 
  8 struct point
  9 {
 10     double x, y;
 11     point( double _x = 0, double _y = 0 )
 12     {
 13         x = _x;
 14         y = _y;
 15     }
 16     point operator-( point t )
 17     {
 18         return point( x - t.x, y - t.y );
 19     }
 20     double operator*( point t )
 21     {
 22         return x * t.y - y * t.x;
 23     }
 24 };
 25 
 26 //快速排斥试验模板
 27 bool quickExclude( point a, point b, point c, point d )
 28 {
 29     int x1 = a.x, x2 = b.x, x3 = c.x, x4 = d.x;
 30     int y1 = a.y, y2 = b.y, y3 = c.y, y4 = d.y;
 31     if (  min(x1,x2) <= max(x3,x4) && min(x3,x4) <= max(x1,x2) &&
 32            min(y1,y2) <= max(y3,y4) && min(y3,y4) <= max(y1,y2)      )
 33            return true;
 34     else    return false;
 35 }
 36 
 37 //跨立试验模板(两线段(ab)和(cd)是否相交)
 38 bool ifIntersect( point a, point b, point c, point d )
 39 {
 40     if ( quickExclude( a, b, c, d ) )
 41     {
 42         if (  ( ( a - c ) * ( c - d ) ) * ( ( b - c ) * ( c - d ) ) <= 0 && ( ( c - a ) * ( a - b ) ) * ( ( d - a ) * ( a - b ) ) <= 0  )
 43         return true;
 44     }
 45     return false;
 46 }
 47 
 48 set<int> ss;
 49 int father[1010];
 50 int num[1010];
 51 point arr[1010][2];
 52 
 53 int main()
 54 {
 55     int T;
 56     cin>>T;
 57     for(int y=0;y<T;y++)
 58     {
 59         if(y!=0)
 60             cout<<endl;
 61         int n;
 62         for(int i=1;i<=1000;i++)
 63             father[i]=i;
 64         for(int i=1;i<=1000;i++)
 65             num[i]=1;
 66         cin>>n;
 67         int time=1;
 68         char c;
 69         double x1,y1,x2,y2;
 70         for(int i=0;i<n;i++)
 71         {
 72             ss.clear();
 73             cin>>c;
 74             if(c=='P')
 75             {
 76                 cin>>x1>>y1>>x2>>y2;
 77                 arr[time][0].x=x1;
 78                 arr[time][0].y=y1;
 79                 arr[time][1].x=x2;
 80                 arr[time][1].y=y2;
 81                 for(int t=1;t<time;t++)
 82                 {
 83                     if(ifIntersect(arr[time][0],arr[time][1],arr[t][0],arr[t][1]))
 84                     {
 85                         ss.insert(time);
 86                         int ft=t;
 87                         while(ft!=father[ft])
 88                         {
 89                             ft=father[ft];
 90                         }
 91                         father[ft]=time;
 92                         if(ss.find(ft)==ss.end())
 93                         {
 94                             ss.insert(ft);
 95                             num[time]=num[time]+num[ft];
 96                             num[ft]=0;
 97                         }
 98                     }
 99                 }
100                 time++;
101             }
102             else if(c=='Q')
103             {
104                 int se;
105                 cin>>se;
106                 int fse=se;
107                 while(fse!=father[fse])
108                 {
109                     fse=father[fse];
110                 }
111                 int j=se;
112                 while(j!=fse)
113                 {
114                     int tmp=father[j];
115                     father[j]=fse;
116                     j=tmp;
117                 }
118                 cout<<num[fse]<<endl;
119             }
120         }
121     }
122     return 0;
123 }
View Code

 

posted @ 2015-08-10 19:57  相儒以沫  阅读(98)  评论(0编辑  收藏  举报