ZOJ 2334 Monkey King (left Tree)

Source: Internet
Author: User
Tags bitset

ZOJ 2334 Monkey King (left Tree)

Idea: left partial tree template question. You need to maintain the set relationship. This can be used to query the set for easy maintenance. In addition, you need to use an array to maintain the left partial tree number of each vertex.

For details, see the code:

#include
 
  #include
  
   #include#include
   
    #include
    
     #include
     
      #include
      
       #include
       
        #include
        
         #include
         
          #include
          
           #include
           
            #include
            
             #include
             #include
              
               #define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 3e5 + 10;int T,n,m;int tot, u, a, b, v[maxn], l[maxn], r[maxn], d[maxn], p[maxn], id[maxn];int _find(int x) { return p[x] == x ? x : p[x] = _find(p[x]); }int Merge(int x, int y) { if(!x) return y; if(!y) return x; if(v[x] < v[y]) swap(x, y); r[x] = Merge(r[x], y); if(d[l[x]] < d[r[x]]) swap(l[x], r[x]); d[x] = d[r[x]] + 1; return x;}int init(int x) { tot++; v[tot] = x; l[tot] = r[tot] = d[tot] = 0; return tot;}int Insert(int x, int y) { return Merge(x, init(y));}int top(int x) { return v[x];}int pop(int x) { return Merge(l[x], r[x]);}int main() { while(~scanf("%d",&n)) { tot = 0; for(int i = 1; i <= n; i++) { p[i] = i; id[i] = i; } for(int i = 1; i <= n; i++) { scanf("%d",&u); init(u); } scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); int x = _find(a), y = _find(b); if(x == y) printf("-1\n"); else { int u = top(id[x]); id[x] = pop(id[x]); id[x] = Insert(id[x], u/2); u = top(id[y]); id[y] = pop(id[y]); id[y] = Insert(id[y], u/2); p[x] = y; id[y] = Merge(id[x], id[y]); u = top(id[y]); printf("%d\n",u); } } } return 0;}
              
            
           
          
         
        
       
      
     
    
   
  
 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.