题意:在一个森林里住着N(N<=10000)只猴子。在一开始,他们是互不认识的。但是随着时间的推移,猴子们少不了争斗,但那只会发生在互不认识
(认识具有传递性)的两只猴子之间。争斗时,两只猴子都会请出他认识的猴子里最强壮的一只(有可能是他自己)进行争斗。争斗后,这两只猴子就互相认识。
每个猴子有一个强壮值,但是被请出来的那两只猴子进行争斗后,他们的强壮值都会减半(例如10会减为5,5会减为2)。现给出每个猴子的初始强壮值,
给出M次争斗,如果争斗的两只猴子不认识,那么输出争斗后两只猴子的认识的猴子里最强壮的猴子的强壮值,否则输出 -1。
析:我们可以通过并查集来判断是不是同一群,然后由于要改值和合并,要有一个合适的数据结构来维护,很容易想到是用左偏树,不过好像Splay也行。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 100000 + 10; const int mod = 1e6 + 10; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } struct Node{ int key, l, r, fa, d; }; Node tr[maxn]; int iroot(int i){ if(i == -1) return i; while(tr[i].fa != -1) i = tr[i].fa; return i; } int Merge(int rx, int ry){ if(rx == -1) return ry; if(ry == -1) return rx; if(tr[rx].key < tr[ry].key) swap(rx, ry); int r = Merge(tr[rx].r, ry); tr[rx].r = r; tr[r].fa = rx; if(tr[r].d > tr[tr[rx].l].d) swap(tr[rx].r, tr[rx].l); if(tr[rx].r == -1) tr[rx].d = 0; else tr[rx].d = tr[tr[rx].r].d + 1; return rx; } int Insert(int i, int key, int root){ tr[i].key = key; tr[i].l = tr[i].r = tr[i].fa = -1; tr[i].d = 0; return Merge(root, i); } int del(int i){ if(i == -1) return -1; int l = tr[i].l, r = tr[i].r, y = tr[i].fa; tr[i].l = tr[i].r = tr[i].fa = -1; int x; tr[x = Merge(l, r)].fa = y; if(y != -1 && tr[y].l == i) tr[y].l = x; if(y != -1 && tr[y].r == i) tr[y].r = x; for( ; y != -1; x = y, y = tr[y].fa){ if(tr[tr[y].l].d < tr[tr[y].r].d) swap(tr[y].l, tr[y].r); if(tr[y].d == tr[tr[y].r].d + 1) break; tr[y].d = tr[tr[y].r].d + 1; } if(x != -1) return iroot(x); return iroot(y); } Node top(int root){ return tr[root]; } int add(int i){ if(i == -1) return i; if(tr[i].l == -1 && tr[i].r == -1 && tr[i].fa == -1){ tr[i].key /= 2; return i; } int key = tr[i].key / 2; int rt = del(i); return Insert(i, key, rt); } void init(){ for(int i = 1; i <= n; ++i){ p[i] = i; scanf("%d", &tr[i].key); tr[i].l = tr[i].r = tr[i].fa = -1; tr[i].d = 0; } } int main(){ while(scanf("%d", &n) == 1){ init(); scanf("%d", &m); while(m--){ int u, v; scanf("%d %d", &u, &v); int x = Find(u); int y = Find(v); if(x == y){ printf("-1\n"); continue; } p[y] = x; int rt = iroot(u); int rt1 = iroot(v); rt = add(rt); rt1 = add(rt1); int t = Merge(rt, rt1); printf("%d\n", tr[t].key); } } return 0; }