QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#766643 | #9570. Binary Tree | OldMemory | TL | 1ms | 3548kb | C++20 | 2.9kb | 2024-11-20 18:02:55 | 2024-11-20 18:02:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> PII;
typedef long long LL;
const int INF = 0x3f3f3f3f3f3f3f3f;
bool multi = 1;
int query(int u, int v) {
cout << "? " << u << ' ' << v << ' ' << endl;
int x;
cin >> x;
return x;
}
void answer(int x) {
cout << "! " << x << endl;
}
void solve() {
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
for(int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
for(auto t : {x, y}) {
if(t == 0) continue;
adj[i].push_back(t);
adj[t].push_back(i);
}
}
vector<bool> st(n + 1);
int bg = 1;
vector<int> tmp;
auto dfs1 = [&](auto self, int u, int father) -> void {
tmp.push_back(u);
for(auto v: adj[u]) {
if(v == father || st[v]) continue;
self(self, v, u);
}
};
vector<int> sz(n + 1), mx(n + 1);
int rt, tot;
auto dfs2 = [&](auto self, int u, int father) -> void {
sz[u] = 1, mx[u] = 0;
for(auto v: adj[u]) {
if(v == father || st[v]) continue;
self(self, v, u);
sz[u] += sz[v];
mx[u] = max(mx[u], sz[v]);
}
mx[u] = max(mx[u], tot - sz[u]);
if(mx[rt] > mx[u]) rt = u;
};
while(1) {
tmp.clear();
dfs1(dfs1, bg, -1);
tot = tmp.size();
mx[rt = 0] = INF;
dfs2(dfs2, bg, -1);
vector<int> son;
for(auto v: adj[rt]) {
if(st[v]) continue;
son.push_back(v);
}
if(son.size() == 0) {
answer(rt);
return;
}
if(son.size() == 1) {
int x = query(rt, son[0]);
if(x == 0) {
answer(rt);
return;
}else if(x == 2) {
answer(son[0]);
return;
}else{
while(1) {}
}
}else if(son.size() == 2) {
int x = query(son[0], son[1]);
if(x == 0) {
bg = son[0];
st[rt] = 1;
}else if(x == 1) {
answer(rt);
return;
}else{
bg = son[1];
st[rt] = 1;
}
}else{
int x = query(son[0], son[1]);
if(x == 0) {
bg = son[0];
st[rt] = 1;
}else if(x == 1) {
bg = rt;
st[son[0]] = st[son[1]] = 1;
}else{
bg = son[1];
st[rt] = 1;
}
}
}
}
signed main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T = 1;
if(multi) cin >> T;
while(T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3548kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0 2
output:
? 1 5 ? 2 4 ! 3 ? 2 1 ! 1
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 2 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 2 5 4 5 3 1 0 0 0 0 0 0 1 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 1 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 5 ? 2 7 ? 1 2 ! 2 ? 5 3 ? 1 4 ? 3 2 ! 3 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 4 5 ! 5 ? 1 2 ? 3 5 ! 3 ? 3 2 ! 2 ? 2 1 ! 2 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 10 9 ! 9 ? 2 1 ! 2 ? 5 9 ? 4 8 ? 5 3 ! 5 ? 5 8 ? 7 1 ? 5 3 ! 5 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 2 1 !...