Advertisement
Guest User

SPOJ LEGO

a guest
Jun 28th, 2010
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. //THIS CODE HAS BEEN WRITTEN BY CODEWORRIOR(SHIVMITRA MISHRA)
  2. #include<stdio.h>
  3. #include<math.h>
  4. #include<stdlib.h>
  5. #include<cstring>
  6. #include<iostream>
  7. #include<map>
  8. #include<vector>
  9. #include<set>
  10. #include<algorithm>
  11. #include<string>
  12. #include<queue>
  13. #include<stack>
  14.  
  15. using namespace std;
  16.  
  17. typedef long long int64;
  18.  
  19. #define FOR(i,a,b) for(int64 i=a;i<=b;i++)
  20. #define A first
  21. #define B second
  22. #define PII pair<int,int>
  23. #define VI vector<int>
  24. #define CLR(a,b) memset(a,b,sizeof(a))
  25. #define mp make_pair
  26. #define pb push_back
  27.  
  28. //DISJOINT SET DATA STRUCTURE IMPLEMENTED BY CODEWORRIOR(SHIVMITRA MISHRA).
  29.  
  30.  
  31.  
  32.  
  33. class DS                               //DATA STRUCTURE FOR DISJOINT SET
  34. {
  35.       private:
  36.               vector<int64>p;
  37.               vector<int64>rank;
  38.       public:
  39.              void make_set(int64 x);
  40.              void set_union(int64 x,int64 y);
  41.              int64 find_set(int64 x);
  42.              void set_size(int64 size);
  43.              void clear_forest();
  44.       private:
  45.               void link(int64 x,int64 y);
  46. }ds;                                   //OBJECT NAME TO BE USED IS ds
  47.  
  48. void DS::clear_forest()
  49. {
  50.      DS::p.clear();
  51.      DS::rank.clear();
  52. }
  53.  
  54. void DS::set_size(int64 size)            //FUNCTION TO SET SIZES OF VECTOR RANK AND PARENT
  55. {
  56.      DS::p.resize(size);
  57.      DS::rank.resize(size);
  58. }
  59. void DS::make_set(int64 x)               //FUNCTION TO MAKE SET
  60. {
  61.      DS::p[x]=x;
  62.      DS::rank[x]=0;
  63. }
  64. void DS::link(int64 x,int64 y)
  65. {
  66.      if(DS::rank[x]>DS::rank[y])
  67.          DS::p[y]=x;
  68.      else
  69.          {
  70.                 DS::p[x]=y;    
  71.                 if(DS::rank[x]==rank[y])
  72.                          DS::rank[y]=DS::rank[y]+1;
  73.          }
  74. }
  75. void DS::set_union(int64 x,int64 y)         //FUNVTION FOR UNION
  76. {
  77.      DS::link(find_set(x),find_set(y));
  78. }
  79. int64 DS::find_set(int64 x)
  80. {
  81.     if(x!=DS::p[x])
  82.            DS::p[x]=find_set(p[x]);
  83.     return DS::p[x];
  84. }
  85.  
  86. //DS ENDS HERE
  87.  
  88.  
  89. int main()
  90. {
  91.     int64 n;
  92.     scanf("%lld",&n);
  93.     ds.set_size(n+1);
  94.     int x1[n],y1[n],x2[n],y2[n];
  95.     FOR(i,0,n-1)
  96.     {
  97.                 scanf("%lld%lld%lld%lld",&x1[i],&y1[i],&x2[i],&y2[i]);
  98.                 ds.make_set(i);
  99.     }
  100.     int64 count=n;
  101.     FOR(i,0,n-2)
  102.     {
  103.                 FOR(j,i,n-1)
  104.                 {        
  105.                             //int j=i+1;
  106.                             if(y1[i]==y2[j] || y1[j]==y2[i])
  107.                             {
  108.                                            int union_len=max(max(x1[i],x2[i]),max(x1[j],x2[j]) ) - min( min(x1[i],x2[i]),min(x1[j],x2[j]) );
  109.                                            int len=abs(x1[i]-x2[i]) + abs(x1[j]-x2[j]);
  110.                                            if(union_len<len)
  111.                                            {
  112.                                                             if(ds.find_set(i)!=ds.find_set(j))
  113.                                                             {
  114.                                                                                         ds.set_union(i,j);
  115.                                                                                         count--;
  116.                                                             }
  117.                                            }
  118.                             }
  119.                 }
  120.     }
  121.    
  122.     printf("%lld",count);
  123.     //system("pause");
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement