#pragma region Macros //#pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma comment(linker, "/stack:200000000") #ifdef ONLINE_JUDGE // #pragma GCC optimize("unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2,tune=native") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,fma,abm,mmx,avx,avx2") // #pragma GCC target("avx2") #endif #pragma GCC optimize("Ofast") #ifdef noimi #define oj_local(a, b) b #else #define oj_local(a, b) a #endif #define LOCAL if(oj_local(0, 1)) #define OJ if(oj_local(1, 0)) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ull = unsigned long long int; using i128 = __int128_t; using pii = pair; using pll = pair; using ld = long double; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; using vi = vc; using vl = vc; using vpi = vc; using vpl = vc; template using pq = priority_queue; template using pqg = priority_queue, greater>; template int si(const T &x) { return x.size(); } template inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); } template inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); } vi iota(int n) { vi a(n); return iota(a.begin(), a.end(), 0), a; } template vi iota(const vector &a, bool greater = false) { vi res(a.size()); iota(res.begin(), res.end(), 0); sort(res.begin(), res.end(), [&](int i, int j) { if(greater) return a[i] > a[j]; return a[i] < a[j]; }); return res; } // macros #define overload5(a, b, c, d, e, name, ...) name #define overload4(a, b, c, d, name, ...) name // #define endl '\n' #define rep0(n) for(int jidlsjf = 0; jidlsjf < n; ++jidlsjf) #define rep1(i, n) for(int i = 0; i < (n); ++i) #define rep2(i, a, b) for(int i = (a); i < (b); ++i) #define rep3(i, a, b, c) for(int i = (a); i < (b); i += (c)) #define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__) #define per0(n) for(int jidlsjf = 0; jidlsjf < n; ++jidlsjf) #define per1(i, n) for(ll i = (n)-1; i >= 0; --i) #define per2(i, a, b) for(ll i = (a)-1; i >= b; --i) #define per3(i, a, b, c) for(ll i = (a)-1; i >= b; i -= c) #define per(...) overload4(__VA_ARGS__, per3, per2, per1, per0)(__VA_ARGS__) #define fore0(a) rep(a.size()) #define fore1(i, a) for(auto &&i : a) #define fore2(a, b, v) for(auto &&[a, b] : v) #define fore3(a, b, c, v) for(auto &&[a, b, c] : v) #define fore4(a, b, c, d, v) for(auto &&[a, b, c, d] : v) #define fore(...) overload5(__VA_ARGS__, fore4, fore3, fore2, fore1, fore0)(__VA_ARGS__) #define fi first #define se second #define pb push_back #define ppb pop_back #define ppf pop_front #define eb emplace_back #define drop(s) cout << #s << endl, exit(0) #define si(c) (int)(c).size() #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define all(c) begin(c), end(c) #define rall(c) rbegin(c), rend(c) #define SORT(v) sort(all(v)) #define REV(v) reverse(all(v)) #define UNIQUE(x) SORT(x), x.erase(unique(all(x)), x.end()) template T SUM(const S &v) { return accumulate(all(v), T(0)); } #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define overload2(_1, _2, name, ...) name #define vec(type, name, ...) vector name(__VA_ARGS__) #define vv(type, name, h, ...) vector> name(h, vector(__VA_ARGS__)) #define vvv(type, name, h, w, ...) vector>> name(h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name(a, vector>>(b, vector>(c, vector(__VA_ARGS__)))) constexpr pii dx4[4] = {pii{1, 0}, pii{0, 1}, pii{-1, 0}, pii{0, -1}}; constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; namespace yesno_impl { const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; const string firstsecond[2] = {"second", "first"}; const string FirstSecond[2] = {"Second", "First"}; const string possiblestr[2] = {"impossible", "possible"}; void YES(bool t = 1) { cout << YESNO[t] << endl; } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { cout << YesNo[t] << endl; } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { cout << yesno[t] << endl; } void no(bool t = 1) { yes(!t); } void first(bool t = 1) { cout << firstsecond[t] << endl; } void First(bool t = 1) { cout << FirstSecond[t] << endl; } void possible(bool t = 1) { cout << possiblestr[t] << endl; } }; // namespace yesno_impl using namespace yesno_impl; #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ IN(name) #define VEC2(type, name1, name2, size) \ vector name1(size), name2(size); \ for(int i = 0; i < size; i++) IN(name1[i], name2[i]) #define VEC3(type, name1, name2, name3, size) \ vector name1(size), name2(size), name3(size); \ for(int i = 0; i < size; i++) IN(name1[i], name2[i], name3[i]) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ IN(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template T ceil(T x, S y) { assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template T floor(T x, S y) { assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } template T POW(T x, int n) { T res = 1; for(; n; n >>= 1, x *= x) if(n & 1) res *= x; return res; } template T POW(T x, S n, const ll &mod) { T res = 1; x %= mod; for(; n; n >>= 1, x = x * x % mod) if(n & 1) res = res * x % mod; return res; } vector factor(ll x) { vector ans; for(ll i = 2; i * i <= x; i++) if(x % i == 0) { ans.push_back({i, 1}); while((x /= i) % i == 0) ans.back().second++; } if(x != 1) ans.push_back({x, 1}); return ans; } template vector divisor(T x) { vector ans; for(T i = 1; i * i <= x; i++) if(x % i == 0) { ans.pb(i); if(i * i != x) ans.pb(x / i); } return ans; } template void zip(vector &x) { vector y = x; UNIQUE(y); for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template void fold_in(vector &v) {} template void fold_in(vector &v, Head &&a, Tail &&...tail) { for(auto e : a) v.emplace_back(e); fold_in(v, tail...); } template void renumber(vector &v) {} template void renumber(vector &v, Head &&a, Tail &&...tail) { for(auto &&e : a) e = lb(v, e); renumber(v, tail...); } template vector zip(vector &head, Args &&...args) { vector v; fold_in(v, head, args...); sort(all(v)), v.erase(unique(all(v)), v.end()); renumber(v, head, args...); return v; } template vector RUI(const vector &v) { vector res(v.size() + 1); for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i]; return res; } // x in [l, r) template bool inc(const T &x, const S &l, const S &r) { return l <= x and x < r; } constexpr ll ten(int n) { return n == 0 ? 1 : ten(n - 1) * 10; } // bit 演算系 ll pow2(int i) { return 1LL << i; } int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); } // int allbit(int n) { return (1 << n) - 1; } constexpr ll mask(int n) { return (1LL << n) - 1; } // int popcount(signed t) { return __builtin_popcount(t); } // int popcount(ll t) { return __builtin_popcountll(t); } int popcount(uint64_t t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } ll rrnd(ll l, ll r) { //[l, r) #ifdef noimi static mt19937_64 gen; #else static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); #endif return uniform_int_distribution(l, r - 1)(gen); } ll rrnd(ll n) { return rrnd(0, n); } // template void random_shuffle(vc &a) { rep(i, si(a)) swap(a[i], a[rnd(0, i + 1)]); } int in() { int x; cin >> x; return x; } ll lin() { unsigned long long x; cin >> x; return x; } template pair operator-(const pair &x, const pair &y) { return pair(x.fi - y.fi, x.se - y.se); } template pair operator+(const pair &x, const pair &y) { return pair(x.fi + y.fi, x.se + y.se); } template pair operator&(const pair &l, const pair &r) { return pair(max(l.fi, r.fi), min(l.se, r.se)); } template pair operator+=(pair &l, const pair &r) { return l = l + r; } template pair operator-=(pair &l, const pair &r) { return l = l - r; } template bool intersect(const pair &l, const pair &r) { return (l.se < r.se ? r.fi < l.se : l.fi < r.se); } template struct edge { int from, to; T cost; int id; edge(int to, T cost) : from(-1), to(to), cost(cost) {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id) {} constexpr bool operator<(const edge &rhs) const noexcept { return cost < rhs.cost; } edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } friend ostream operator<<(ostream &os, edge &e) { return os << e.to; } }; template using Edges = vector>; using Tree = vector>; using Graph = vector>; template using Wgraph = vector>>; Graph getG(int n, int m = -1, bool directed = false, int margin = 1) { Tree res(n); if(m == -1) m = n - 1; while(m--) { int a, b; cin >> a >> b; a -= margin, b -= margin; res[a].emplace_back(b); if(!directed) res[b].emplace_back(a); } return res; } Graph getTreeFromPar(int n, int margin = 1) { Graph res(n); for(int i = 1; i < n; i++) { int a; cin >> a; res[a - margin].emplace_back(i); } return res; } template Wgraph getWg(int n, int m = -1, bool directed = false, int margin = 1) { Wgraph res(n); if(m == -1) m = n - 1; while(m--) { int a, b; T c; scan(a), scan(b), scan(c); a -= margin, b -= margin; res[a].emplace_back(b, c); if(!directed) res[b].emplace_back(a, c); } return res; } void add(Graph &G, int x, int y) { G[x].eb(y), G[y].eb(x); } template void add(Wgraph &G, int x, int y, T c) { G[x].eb(y, c), G[y].eb(x, c); } #define TEST \ INT(testcases); \ while(testcases--) namespace aux { template struct tp { static void output(std::ostream &os, const T &v) { os << std::get(v) << (&os == &cerr ? ", " : " "); tp::output(os, v); } }; template struct tp { static void output(std::ostream &os, const T &v) { os << std::get(v); } }; } // namespace aux template std::ostream &operator<<(std::ostream &os, const std::tuple &t) { if(&os == &cerr) { os << '('; } aux::tp, 0, sizeof...(Ts) - 1>::output(os, t); if(&os == &cerr) { os << ')'; } return os; } template ostream &operator<<(ostream &os, const pair &p) { if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; } return os << p.first << " " << p.second; } template std::basic_ostream &operator<<(std::basic_ostream &os, const Container &x) { bool f = true; if(&os == &cerr) os << "["; for(auto &y : x) { if(&os == &cerr) os << (f ? "" : ", ") << y; else os << (f ? "" : " ") << y; f = false; } if(&os == &cerr) os << "]"; return os; } #ifdef noimi #undef endl void debug() { cerr << endl; } void debug(bool) { cerr << endl; } template void debug(bool is_front, Head head, Tail... tail) { if(!is_front) cerr << ", "; cerr << head; debug(false, tail...); } #define dump(args...) \ { \ vector _debug = _split(#args, ','); \ err(true, begin(_debug), args); \ } vector _split(const string &s, char c) { vector v; stringstream ss(s); string x; while(getline(ss, x, c)) { if(empty(v)) v.eb(x); else { bool flag = false; for(auto [c, d] : {pair('(', ')'), pair('[', ']'), pair('{', '}')}) { if(count(all(v.back()), c) != count(all(v.back()), d)) flag = true; } if(flag) v.back() += "," + x; else v.eb(x); } } return move(v); } void err(bool, vector::iterator) { cerr << endl; } template void err(bool is_front, vector::iterator it, T a, Args... args) { if(!is_front) cerr << ", "; cerr << it->substr((*it)[0] == ' ', (*it).size()) << " = " << a, err(false, ++it, args...); } // #define dump(...) cerr << #__VA_ARGS__ << " : ", debug(true, __VA_ARGS__) #else #define dump(...) static_cast(0) #define dbg(...) static_cast(0) #endif void OUT() { cout << endl; } template void OUT(const Head &head, const Tail &...tail) { cout << head; if(sizeof...(tail)) cout << ' '; OUT(tail...); } template static constexpr T inf = numeric_limits::max() / 2; template struct REC { F f; REC(F &&f_) : f(std::forward(f_)) {} template auto operator()(Args &&...args) const { return f(*this, std::forward(args)...); } }; template vector> runLength(const vector &v) { vector> res; for(auto &e : v) { if(res.empty() or res.back().fi != e) res.eb(e, 1); else res.back().se++; } return res; } vector> runLength(const string &v) { vector> res; for(auto &e : v) { if(res.empty() or res.back().fi != e) res.eb(e, 1); else res.back().se++; } return res; } int toint(const char &c, const char start = 'a') { return c - start; } int toint(const char &c, const string &chars) { return find(all(chars), c) - begin(chars); } int alphabets_to_int(const char &c) { return (islower(c) ? c - 'a' : c - 'A' + 26); } template auto toint(const T &v, const char &start = 'a') { vector ret; ret.reserve(v.size()); for(auto &&e : v) ret.emplace_back(toint(e, start)); return ret; } template auto toint(const T &v, const string &start) { vector ret; ret.reserve(v.size()); for(auto &&e : v) ret.emplace_back(toint(e, start)); return ret; } // a -> 0, A -> 26 template auto alphabets_to_int(const T &s) { vector res; res.reserve(s.size()); for(auto &&e : s) { res.emplace_back(alphabets_to_int(e)); } return res; } template T bin_search(T ok, T ng, const F &f) { while(abs(ok - ng) > 1) { T mid = ok + ng >> 1; (f(mid) ? ok : ng) = mid; } return ok; } template T bin_search_double(T ok, T ng, const F &f, int iter = 80) { while(iter--) { T mid = (ok + ng) / 2; (f(mid) ? ok : ng) = mid; } return ok; } struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(11); } } setup_io; #pragma endregion // 0 以上 2 ^ (6D) 未満の整数を取れる // D : 3 -> 2.6 * 10 ^ 5 // D : 4 -> 1.6 * 10 ^ 7 // D : 5 -> 1.0 * 10 ^ 9 // D : 6 -> int の範囲全部 template struct FastSet { int n; using u64 = uint64_t; vector a[D]; FastSet(int n_ = (1 << std::min(31, D * 6))) : n(n_) { for(int i = 0; i < D; i++) { n_ = (n_ + 63) >> 6; a[i].assign(n_, 0); } } bool empty() const { return !a[D - 1][0]; } bool contains(int x) const { return (a[0][x >> 6] >> (x & 63)) & 1; } void insert(int x) { for(int i = 0; i < D; i++) { const int y = x & 63; x >>= 6; a[i][x] |= 1ULL << y; } } void erase(int x) { for(int i = 0; i < D; i++) { const int y = x & 63; x >>= 6; if((a[i][x] &= ~(1ULL << y))) break; } } int next(int x) const { for(int i = 0; i < D; i++) { const int k = x >> 6, y = x & 63; if(k >= a[i].size()) return n; const u64 top = a[i][k] >> y; if(top) { x += __builtin_ctzll(top); for(int j = i - 1; j >= 0; --j) x = x << 6 | __builtin_ctzll(a[j][x]); return x; } x = k + 1; } return n; } int prev(int x) const { for(int i = 0; i < D; ++i) { if(x < 0) return -1; const int k = x >> 6, y = x & 63; const u64 bot = a[i][k] << (63 - y); if(bot) { x -= __builtin_clzll(bot); for(int j = i - 1; j >= 0; --j) x = x << 6 | (63 - __builtin_clzll(a[j][x])); return x; } x = k - 1; } return -1; } int max() const { return prev(n); } int min() const { return next(0); } }; template struct HLDecomposition { G &g; vector sz, in, out, head, rev, par, d, vis, roots; HLDecomposition(G &g, vector r = vector()) : g(g), d(g.size()), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) { if(empty(r)) { vector vis(g.size()); for(int i = 0; i < g.size(); i++) { if(vis[i]) continue; roots.emplace_back(i); queue q; q.emplace(i); while(!empty(q)) { int x = q.front(); vis[x] = true; q.pop(); for(auto e : g[x]) { if(!vis[e]) q.emplace(e); } } } } else roots = r; } void dfs_sz(int idx, int p) { par[idx] = p; sz[idx] = 1; if(g[idx].size() and g[idx][0] == p) swap(g[idx][0], g[idx].back()); for(auto &to : g[idx]) { if(to == p) continue; d[to] = d[idx] + 1; dfs_sz(to, idx); sz[idx] += sz[to]; if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to); } } void dfs_hld(int idx, int par, int ×) { in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]) { if(to == par) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times); } out[idx] = times; } template void dfs_hld(int idx, int par, int ×, vector &v) { in[idx] = times++; rev[in[idx]] = idx; for(auto &to : g[idx]) { if(to == par) { v[in[idx]] = to.cost; continue; } head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times, v); } out[idx] = times; } template void dfs_hld(int idx, int par, int ×, vector &v, vector &a) { in[idx] = times++; rev[in[idx]] = idx; v[in[idx]] = a[idx]; for(auto &to : g[idx]) { if(to == par) continue; head[to] = (g[idx][0] == to ? head[idx] : to); dfs_hld(to, idx, times, v, a); } out[idx] = times; } void build() { int t = 0; for(auto root : roots) { head[root] = root; dfs_sz(root, -1); dfs_hld(root, -1, t); } } template vector build(int root = 0) { vector res(g.size()); int t = 0; for(auto root : roots) { head[root] = root; dfs_sz(root, -1); dfs_hld(root, -1, t, res); } return res; } template vector build(vector &a, int root = 0) { vector res(g.size()); for(auto root : roots) { head[root] = root; dfs_sz(root, -1); int t = 0; dfs_hld(root, -1, t, res, a); } return res; } int la(int v, int k) { while(1) { int u = head[v]; if(in[v] - k >= in[u]) return rev[in[v] - k]; k -= in[v] - in[u] + 1; v = par[u]; } } int lca(int u, int v) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) return u; } } template T query(int u, int v, const T &e, const Q &q, const F &f, bool edge = false) { T l = e, r = e; for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v), swap(l, r); if(head[u] == head[v]) break; l = f(q(in[head[v]], in[v] + 1), l); } return f(f(q(in[u] + edge, in[v] + 1), l), r); } template T query(int u, int v, const T &e, const Q &q1, const Q2 &q2, const F &f, bool edge = false) { T l = e, r = e; for(;;) { if(head[u] == head[v]) break; if(in[u] > in[v]) { l = f(l, q2(in[head[u]], in[u] + 1)); u = par[head[u]]; } else { r = f(q1(in[head[v]], in[v] + 1), r); v = par[head[v]]; } } if(in[u] > in[v]) return f(f(l, q2(in[v] + edge, in[u] + 1)), r); return f(f(l, q1(in[u] + edge, in[v] + 1)), r); } template void add(int u, int v, const Q &q, bool edge = false) { for(;; v = par[head[v]]) { if(in[u] > in[v]) swap(u, v); if(head[u] == head[v]) break; q(in[head[v]], in[v] + 1); } q(in[u] + edge, in[v] + 1); } constexpr int operator[](int k) { return in[k]; } constexpr int dist(int u, int v) { return d[u] + d[v] - 2 * d[lca(u, v)]; } // u -> v の unique path vector road(int u, int v) { int l = lca(u, v); vector a, b; for(; v != l; v = par[v]) b.eb(v); for(; u != l; u = par[u]) a.eb(u); a.eb(l); per(i, si(b), 0) a.eb(b[i]); return a; } }; int main() { INT(n); auto g = getG(n); HLDecomposition hld(g); hld.build(); vector isp_light(n); auto &par = hld.par; vi val = iota(n), inv = iota(n); vector> s(n); vi leaf(n); rep(i, 1, n) if(si(g[i]) == 1) leaf[i] = 1; FastSet<3> fs(n), fs2(n); rep(i, 1, n) { if(g[par[i]][0] != i) { isp_light[i] = true; s[par[i]].emplace(i, i); } } rep(i, 1, n) if(si(g[i]) == 1) fs.insert(hld[i]); fs2.insert(hld[0]); rep(i, n) if(isp_light[i]) fs2.insert(hld[i]); auto proc = [&](int x) { if(empty(s[x])) return; auto [v, to] = *s[x].rbegin(); if(v < val[g[x][0]]) fs.erase(hld[x]); else fs.insert(hld[x]); if(isp_light[x]) return; if(par[x] != -1 and v < val[par[x]] and !isp_light[x]) fs2.erase(hld[x]); else fs2.insert(hld[x]); }; rep(i, n) proc(i); auto del = [&](int x) { if(isp_light[x]) s[par[x]].erase(pii(val[x], x)); }; auto ins = [&](int x) { if(isp_light[x]) s[par[x]].emplace(pii(val[x], x)); }; int ans = 0; INT(Q); rep(Q) { INT(u, v); u = (u + n - 1 + ans) % n, v = (v + n - 1 + ans) % n; del(u), del(v); inv[val[u]] = v, inv[val[v]] = u; swap(val[u], val[v]); ins(u), ins(v); proc(u), proc(v); if(u) proc(par[u]); if(v) proc(par[v]); if(!leaf[u]) proc(g[u][0]); if(!leaf[v]) proc(g[v][0]); int heavy = (leaf[u] ? -1 : val[g[u][0]]); int p = (u ? val[par[u]] : -1); int ma = (empty(s[u]) ? max(heavy, p) : max({heavy, p, rbegin(s[u])->se})); // dump(u, v, heavy, p, ma, fs.contains(0)); // dump(val); if(ma == p) { int pre = -1; while(1) { int nu = hld.rev[fs2.prev(hld[u])]; if(nu != u) pre = g[nu][0]; u = nu; if(!u and si(g[0]) == 1) goto LAST; p = (u ? val[par[u]] : -1); auto get = [&](int u) { int kid = (leaf[u] ? -1 : val[g[u][0]]); if(g[u][0] == pre) kid = -1; if(!empty(s[u])) { if(rbegin(s[u])->se != pre) chmax(kid, rbegin(s[u])->fi); else if(si(s[u]) > 1) chmax(kid, next(rbegin(s[u]))->fi); } return kid; }; int kid = get(u); // dump(u, v, pre, p, kid); if(p > kid) { pre = u; u = par[u]; int k = get(u); p = (u ? val[par[u]] : -1); if(p < k) { u = inv[k]; break; } } else { u = inv[kid]; break; } } } while(!leaf[u]) { u = hld.rev[fs.next(hld[u])]; // dump(u); if(leaf[u]) break; u = rbegin(s[u])->se; } LAST: cout << ++u << endl; ans = u; } }