QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#727799 | #9570. Binary Tree | ucup-team5319# | TL | 0ms | 0kb | C++14 | 1.6kb | 2024-11-09 13:51:21 | 2024-11-09 13:51:21 |
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
vector<int> G[MAXN]; bool vis[MAXN]; int siz[MAXN], N;
void getsiz(int u, int f) {
siz[u] = 1; for (int v : G[u]) if (v != f && vis[v] == 0) getsiz(v, u), siz[u] += siz[v];
}
int getg(int u, int f) {
int mx = 0; for (int v : G[u]) if (v != f && vis[v] == 0) {
int x = getg(v, u); if (x) return x;
} return mx * 2 <= N && siz[u] * 2 >= N ? u : 0;
}
int dfz(int u) {
getsiz(u, u); N = siz[u]; int g = getg(u, u); getsiz(g, g); vis[g] = 1;
vector<int> val; for (int x : G[g]) if (vis[x] == 0) val.push_back(x);
if (val.empty()) return g;
if (val.size() == 1) {
printf("? %d %d\n", val[0], g); fflush(stdout);
int w; scanf("%d", &w); return w == 0 ? val[0] : g;
}
if (val.size() == 2) {
printf("? %d %d\n", val[0], val[1]); fflush(stdout);
int w; scanf("%d", &w);
if (w == 1) return g;
else if (w == 0) return dfz(val[0]);
else return dfz(val[1]);
}
if (val.size() == 3) {
if ((siz[val[2]] + 1) * 2 >= N) swap(val[0], val[2]);
printf("? %d %d\n", val[0], val[1]); fflush(stdout);
int w; scanf("%d", &w);
if (w == 1) return vis[val[0]] = vis[val[1]] = 1, vis[g] = 0, dfz(g);
else if (w == 0) return dfz(val[0]);
else return dfz(val[1]);
}
}
int main() {
int t; scanf("%d", &t); while (t--) {
int n; scanf("%d", &n);
for (int i = 1; i <= n; i++) G[i].clear(), vis[i] = 0;
for (int i = 1; i <= n; i++) {
int x; scanf("%d", &x); if (x) G[x].push_back(i), G[i].push_back(x);
scanf("%d", &x); if (x) G[x].push_back(i), G[i].push_back(x);
}
printf("! %d\n", dfz(1));
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0
output:
? 3 5 ? 2 1