QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#763243 | #9570. Binary Tree | flyWang | WA | 109ms | 14576kb | C++23 | 2.4kb | 2024-11-19 19:09:18 | 2024-11-19 19:09:19 |
Judging History
answer
#include<bits/stdc++.h>
#define se second
using namespace std;
const int N = 1e5;
int n;
int del[N];
int root;
vector<int> e[N];
vector<int> V;
int siz[N], mxs[N];
void dfs(int u, int p) {
V.push_back(u);
siz[u] = 1;
mxs[u] = 0;
for (int v : e[u]) if (v != p && !del[v])
dfs(v, u), mxs[u] = max(mxs[u], siz[v]), siz[u] += siz[v];
}
void Del(int u, int p) {
del[u] = 1;
for (int v : e[u]) if (v != p && !del[v])
Del(v, u);
}
void solve() {
cin >> n;
for(int i = 1;i <= n;++i) e[i].clear(), del[i] = 0;
for(int i = 1;i <= n;++i){
int x, y; cin >> x >> y;
if (x) e[i].push_back(x), e[x].push_back(i);
if (y) e[i].push_back(y), e[y].push_back(i);
}
root = 1;
while (true) {
V.clear();
dfs(root, 0);
int tot = (int)V.size();
root = 0;
for (int u : V) {
mxs[u] = max(mxs[u], tot - siz[u]);
if (!root || mxs[root] > mxs[u])
root = u;
}
V.clear();
dfs(root, 0);
if (tot == 1) {
cout << "! " << root << endl;
return ;
}
vector<pair<int, int>> w;
for (int v : e[root]) if (!del[v])
w.push_back({siz[v], v});
sort(w.begin(), w.end());
reverse(w.begin(), w.end());
int deg = (int)w.size();
if (deg == 1) {
cout << "? " << root << ' ' << w[0].se << endl;
int t; cin >> t;
if (t == 0) del[w[0].se] = 1;
else del[root] = 1, root = w[0].se;
} else if (deg == 2) {
cout << "? " << w[0].se << ' ' << w[1].se << endl;
int t; cin >> t;
if (t == 0) Del(root, w[0].se), root = w[0].se;
else if (t == 1) Del(w[0].se, root), Del(w[1].se, root);
else Del(root, w[1].se), root = w[1].se;
} else {
cout << "? " << w[0].se << ' ' << w[1].se << endl;
int t; cin >> t;
if (t == 0) Del(root, w[0].se), root = w[0].se;
else if (t == 1) Del(w[0].se, root), Del(w[1].se, root);
else Del(root, w[1].se), root = w[1].se;
}
}
}
int main() {
// ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t = 1;
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: 3808kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 2 1 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: 0
Accepted
time: 107ms
memory: 4320kb
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 0 0 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 1 2 5 4 5 3 1 0 0 0 0 0 0 0 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 0 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:
? 8 6 ? 4 5 ? 4 3 ! 3 ? 7 2 ? 8 7 ? 8 6 ! 8 ? 8 3 ? 8 4 ? 6 2 ! 2 ? 2 5 ? 2 3 ! 3 ? 5 7 ? 4 1 ! 4 ? 5 1 ? 5 4 ! 4 ? 4 2 ? 4 3 ! 4 ? 3 2 ! 2 ? 1 2 ! 1 ? 3 2 ! 2 ? 7 9 ? 7 4 ? 6 3 ! 3 ? 1 2 ! 1 ? 9 5 ? 2 1 ? 2 6 ! 2 ? 10 3 ? 8 6 ? 4 2 ! 4 ? 9 3 ? 9 7 ? 2 1 ! 2 ? 1 2 ! 2 ? 4 3 ? 7 1 ! 1 ? 4 9 ? 8 4 ? 8...
result:
ok OK (5555 test cases)
Test #3:
score: 0
Accepted
time: 61ms
memory: 3668kb
input:
600 2 2 0 0 0 2 3 2 0 3 0 0 0 2 4 4 0 1 0 0 0 3 0 0 0 5 4 0 0 0 1 0 2 0 3 0 2 0 6 4 0 6 0 2 0 5 0 0 0 1 0 0 0 7 7 0 3 0 6 0 5 0 2 0 1 0 0 0 0 1 8 7 0 0 0 2 0 8 0 1 0 5 0 3 0 6 0 0 0 0 9 7 0 4 0 2 0 1 0 0 0 8 0 9 0 5 0 6 0 2 0 2 10 9 0 6 0 8 0 7 0 0 0 10 0 2 0 4 0 5 0 1 0 0 0 2 11 2 0 10 0 6 0 9 0 0 ...
output:
? 1 2 ! 2 ? 3 1 ! 1 ? 4 2 ? 4 3 ! 4 ? 4 3 ? 3 5 ! 3 ? 6 4 ? 6 3 ! 6 ? 6 2 ? 7 6 ! 1 ? 5 7 ? 8 5 ? 8 4 ! 8 ? 9 1 ? 2 1 ? 2 3 ! 3 ? 2 10 ? 8 7 ? 8 3 ! 3 ? 9 2 ? 10 6 ? 6 5 ! 5 ? 6 1 ? 4 2 ? 11 4 ! 11 ? 3 2 ? 12 7 ? 12 1 ! 10 ? 12 9 ? 11 8 ? 12 11 ! 11 ? 14 2 ? 7 4 ? 4 2 ! 2 ? 13 8 ? 14 10 ? 12 14 ? 12...
result:
ok OK (600 test cases)
Test #4:
score: -100
Wrong Answer
time: 109ms
memory: 14576kb
input:
2 99999 21832 0 77205 0 62668 0 58313 0 14640 0 76941 0 62678 0 8464 0 43145 0 26195 0 46140 0 83205 0 40047 0 81645 0 27077 0 92036 0 14236 0 3576 0 15430 0 75654 0 29049 0 62218 0 83318 0 1116 0 77861 0 9755 0 49236 0 70959 0 62295 0 33580 0 88208 0 55840 0 71061 0 24695 0 88831 0 1891 0 57285 0 9...
output:
? 70790 43991 ? 98065 36882 ? 89400 44312 ? 52881 29266 ? 79632 4792 ? 66257 28530 ? 90776 45250 ? 81001 52265 ? 6951 63174 ? 90092 59116 ? 83815 81715 ? 47823 70694 ? 23914 22782 ? 22177 76082 ? 26370 60472 ? 90092 26370 ! 26370 ? 1 6207 ! 6207
result:
wrong answer There are 89487 candidates. (test case 2)