QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#731016 | #9570. Binary Tree | ucup-team5226# | TL | 1ms | 3864kb | C++20 | 4.9kb | 2024-11-09 23:08:15 | 2024-11-09 23:08:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
using vvvc = vc<vvc<T>>;
#define overload5(a, b, c, d, e, name, ...) name
#define overload4(a, b, c, d, name, ...) name
#define overload3(a, b, c, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = n; i--;)
#define rrep2(i, n) for (ll i = n; i--;)
#define rrep3(i, a, b) for (ll i = b; i-- > (a);)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define each1(i, a) for (auto &&i : a)
#define each2(x, y, a) for (auto &&[x, y] : a)
#define each3(x, y, z, a) for (auto &&[x, y, z] : a)
#define each4(w, x, y, z, a) for (auto &&[w, x, y, z] : a)
#define each(...) overload5(__VA_ARGS__, each4, each3, each2, each1)(__VA_ARGS__)
#define all1(i) begin(i), end(i)
#define all2(i, a) begin(i), begin(i) + a
#define all3(i, a, b) begin(i) + a, begin(i) + b
#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)
#define rall1(i) rbegin(i), rend(i)
#define rall2(i, a) rbegin(i), rbegin(i) + a
#define rall3(i, a, b) rbegin(i) + a, rbegin(i) + b
#define rall(...) overload3(__VA_ARGS__, rall3, rall2, rall1)(__VA_ARGS__)
template <class T>
bool chmin(T &a, const T &b) {
if (a <= b) return 0;
a = b;
return 1;
}
template <class T>
bool chmax(T &a, const T &b) {
if (a >= b) return 0;
a = b;
return 1;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
return chmin(a, (T)b);
}
template <class T, class U>
bool chmax(T &a, const U &b) {
return chmax(a, (T)b);
}
void solve();
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll t = 1;
cin >> t;
for (int i = 1; i <= t; i++) solve();
return 0;
}
ll dy[] = {0, 0, 1, 0, -1}, dx[] = {0, 1, 0, -1, 0};
// ll dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void solve() {
ll n;
cin >> n;
vvc<ll> g(n);
rep(i, n) {
ll l, r;
cin >> l >> r;
l--, r--;
if (l >= 0) g[i].push_back(l), g[l].push_back(i);
if (r >= 0) g[i].push_back(r), g[r].push_back(i);
}
ll r = 0;
while (true) {
vc<ll> dp(n);
auto dfs = [&](auto &&dfs, ll from, ll par) -> void {
dp[from] = 1;
each(to, g[from]) {
if (to == par) continue;
dfs(dfs, to, from);
dp[from] += dp[to];
}
};
dfs(dfs, r, -1);
ll rem = dp[r];
if (rem == 1) break;
dp.assign(n, 0);
auto dfs2 = [&](auto &&dfs, ll from, ll par) -> void {
dp[from] = 1;
each(to, g[from]) {
if (to == par) continue;
dfs(dfs, to, from);
dp[from] += dp[to];
}
};
dfs2(dfs2, r, -1);
vc<ll> vis(n);
ll gra = r;
vis[gra] = 1;
while (true) {
bool ok = true;
for (auto to : g[gra]) {
if (vis[to]) continue;
if (dp[to] * 2 > rem) {
gra = to;
ok = false;
break;
}
}
if (ok) break;
}
ll u = g[gra][0];
ll v = (g[gra].size() >= 2 ? g[gra][1] : gra);
cout << "? " << v + 1 << " " << u + 1 << endl;
ll x;
cin >> x;
if (x == 0) {
r = v;
vc<ll> era;
for (auto to : g[v]) {
if (to == gra) era.push_back(to);
if (to == u) era.push_back(to);
}
sort(all(era));
era.erase(unique(all(era)), era.end());
for (auto e : era) g[v].erase(find(all(g[v]), e));
} else if (x == 1) {
r = gra;
vc<ll> era;
for (auto to : g[gra]) {
if (to == u) era.push_back(to);
if (to == v) era.push_back(to);
}
sort(all(era));
era.erase(unique(all(era)), era.end());
for (auto e : era) g[gra].erase(find(all(g[gra]), e));
} else {
r = u;
vc<ll> era;
for (auto to : g[u]) {
if (to == gra) era.push_back(to);
if (to == v) era.push_back(to);
}
sort(all(era));
era.erase(unique(all(era)), era.end());
for (auto e : era) g[u].erase(find(all(g[u]), e));
}
}
cout << "! " << r + 1 << endl;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3864kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 5 1 ? 4 2 ! 3 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0
output:
? 8 1 ? 4 5 ? 4 3 ! 3