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;}