1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include <bits/stdc++.h>
const int kMaxN = 1e5 + 5;
int n; int f[kMaxN], g[kMaxN], h[kMaxN], dep[kMaxN]; int buc[kMaxN]; std::vector<int> G[kMaxN];
struct SGT { int mx[kMaxN * 4], tag[kMaxN * 4]; void pushup(int x) { mx[x] = std::max(mx[x << 1], mx[x << 1 | 1]); } void addtag(int x, int v) { mx[x] += v, tag[x] += v; } void pushdown(int x) { if (tag[x]) { addtag(x << 1, tag[x]), addtag(x << 1 | 1, tag[x]); tag[x] = 0; } } void build(int x, int l, int r) { mx[x] = -1e9, tag[x] = 0; if (l == r) return; int mid = (l + r) >> 1; build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r); } void update(int x, int l, int r, int ql, int qr, int v) { if (l > qr || r < ql) return; else if (l >= ql && r <= qr) return addtag(x, v); pushdown(x); int mid = (l + r) >> 1; update(x << 1, l, mid, ql, qr, v), update(x << 1 | 1, mid + 1, r, ql, qr, v); pushup(x); } } sgt;
void ins(int x) { if (!buc[x]++) sgt.update(1, 0, n, x, x, 1e9 + x); sgt.update(1, 0, n, 0, x, 1); }
void del(int x) { if (!--buc[x]) sgt.update(1, 0, n, x, x, -1e9 - x); sgt.update(1, 0, n, 0, x, -1); }
int query() { return std::max(sgt.mx[1] - 1, 1); }
void dfs1(int u, int fa) { dep[u] = dep[fa] + 1; bool isleaf = 1; for (auto v : G[u]) { if (v == fa) continue; dfs1(v, u); } std::sort(G[u].begin(), G[u].end(), [&] (int i, int j) { return f[i] > f[j]; }); for (auto v : G[u]) { if (v == fa) continue; ins(f[v]); } f[u] = query(); for (auto v : G[u]) { if (v == fa) continue; del(f[v]); } }
void dfs2(int u, int fa) { if (fa) ins(g[u]); for (auto v : G[u]) { if (v == fa) continue; ins(f[v]); } for (auto v : G[u]) { if (v == fa) continue; del(f[v]), g[v] = query(), ins(f[v]); } for (auto v : G[u]) { if (v == fa) continue; del(f[v]); } if (fa) del(g[u]); for (auto v : G[u]) { if (v == fa) continue; dfs2(v, u); }
std::vector<int> vec; for (auto v : G[u]) { if (v == fa) continue; vec.emplace_back(f[v]); } if (fa) vec.emplace_back(g[u]); std::sort(vec.begin(), vec.end(), std::greater<>()); for (int i = 0; i < vec.size(); ++i) { h[u] = std::max<int>(h[u], vec[i] + vec.size() - 1); if (i) h[u] = std::max(h[u], vec[i] + vec[0] + i - 1); } }
int ask(int x) { std::cout << "? " << x << std::endl; int v; std::cin >> v; return v; }
int solve(int u, int fa) { bool isleaf = 1; for (auto v : G[u]) { if (v == fa) continue; int val = solve(v, u); if (val != u) return val; isleaf = 0; } if (isleaf) return ask(u); else return u; }
void dickdreamer() { std::cin >> n; for (int i = 1; i < n; ++i) { int u, v; std::cin >> u >> v; G[u].emplace_back(v), G[v].emplace_back(u); } sgt.build(1, 0, n); dfs1(1, 0), dfs2(1, 0); std::cout << *std::max_element(h + 1, h + 1 + n) << std::endl; int r, a = 0, b = 0; std::cin >> r; dfs1(r, 0); for (auto v : G[r]) { int val = solve(v, r); if (val != r) { if (!a) a = val; else b = val; } if (a && b) break; } if (!a) a = r; if (!b) b = r; std::cout << "! " << a << ' ' << b << std::endl; }
int32_t main() { #ifdef ORZXKR freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); int T = 1; while (T--) dickdreamer(); return 0; }
|