QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#721881 | #9570. Binary Tree | ucup-team3519# | RE | 1ms | 6632kb | C++17 | 2.3kb | 2024-11-07 17:04:29 | 2024-11-07 17:04:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define V vector
#define pb push_back
typedef pair<int, int> pi;
#define fi first
#define se second
const int INF = 2e9 + 1000;
const int N = 1e5;
int n;
V<int> e[N];
int ban[N];
int d[N], sz[N], f[N];
void dfs(int x, int p) {
f[x] = p;
d[x] = 0;
if(p) d[x]++;
sz[x] = 0;
for(auto y : e[x]) if(y != p && !ban[y]) {
dfs(y, x);
d[x]++;
sz[x] += sz[y];
}
sz[x]++;
}
int cnt = 0;
int query(int x, int y) {
cnt++;
assert(cnt <= __lg(n));
cout << "? " << x << " " << y << endl;
int t; cin >> t;
return t;
}
int deal(int x) {
dfs(x, 0);
int tot = sz[x];
if(tot == 1) return x;
int good = -1;
for(int i = 1; i <= n; i++) if(!ban[i]) {
bool ok = 1;
for(auto y : e[i]) if(y != f[i] && !ban[y]) {
if(sz[y] > tot / 2) ok = 0;
}
if(f[i]) {
if(tot - sz[i] > tot / 2) ok = 0;
}
if(ok) good = i;
}
assert(good != -1);
dfs(good, 0);
assert(d[good] >= 1);
if(d[good] == 1) {
int u = good, v = -1;
for(auto y : e[good]) if(!ban[y]) {
v = y;
}
assert(v != -1);
int t = query(u, v);
if(t == 0) {
return u;
} else return v;
}
int a = 0, b = 0;
for(auto y : e[good]) if(!ban[y]) {
if(sz[y] > sz[a]) {
b = a, a = y;
} else if(sz[y] > sz[b]) {
b = y;
}
}
int t = query(a, b);
if(t == 0) {
ban[good] = 1;
return deal(a);
} else if(t == 2) {
ban[good] = 1;
return deal(b);
} else if(t == 1) {
ban[a] = 1, ban[b] = 1;
return deal(good);
}
}
void solve() {
cin >> n;
for(int i = 1; i <= n; i++) e[i].clear(), ban[i] = 0;
cnt = 0;
for(int i = 1; i <= n; i++) {
int x, y; cin >> x >> y;
if(x) e[i].pb(x), e[x].pb(i);
if(y) e[i].pb(y), e[y].pb(i);
}
int ans = deal(1);
cout << "! " << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 6632kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 5 2 ! 5 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Runtime Error
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 0
output:
? 2 4 ? 2 7