QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#736390 | #9570. Binary Tree | ucup-team4967 | ML | 0ms | 3608kb | C++20 | 2.8kb | 2024-11-12 10:41:32 | 2024-11-12 10:41:32 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1ll << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD (ll)1000000007
#define P pair<int, int>
int dfs_sz(int v, int p, vector<vector<int>> &g, vector<int> &sz, vector<int> &used) {
sz[v] = 1;
for(auto& i : g[v]) {
if(used[i] or i == p) continue;
sz[v] += dfs_sz(i, v, g, sz, used);
if(sz[g[v][0]] < sz[i]) swap(g[v][0], i);
}
return sz[v];
}
P dfs_search(int v, int p, int nsz, vector<vector<int>> &g, vector<int> &sz, vector<int> &used){
ll now = -1;
for(auto& i: g[v]) {
if(used[i] or i == p)continue;
if(sz[i] > nsz / 2)return dfs_search(i, v, nsz, g, sz, used);
if(sz[i] * 2 == nsz) now = i;
}
return P(v, now);
}
void dfs_used(int v, int p, vector<vector<int>> &g, vector<int> &used){
used[v] = true;
for(auto& i: g[v]) {
if(i == p or used[i])continue;
dfs_used(i, v, g, used);
}
}
ll query(P p, vector<vector<int>> &g, vector<int> &sz, vector<int> &used){
// 2
if(p.second != -1){
cout << "? " << p.first + 1 << " " << p.second + 1 << endl;
int a;
cin >> a;
if(a == 0){
dfs_used(p.second, p.first, g, used);
return p.first;
}
else {
dfs_used(p.first, p.second, g, used);
return p.second;
}
}
// 3
int v = p.first;
vector<int> h;
for(auto& i: g[v]) {
if(used[i])continue;
h.push_back(i);
}
//3-0(goal)
if(h.size() == 0) return v - 1000000;
else if(h.size() == 1) { // 3-1
return query(P(v, h[0]), g, sz, used);
}
else { // 3 - 2, 3
cout << "? " << h[0] + 1 <<" " << h[1] + 1<< endl;
int a;
cin >> a;
if(a == 0){ //h[0]
dfs_used(v, h[0], g, used);
return h[0];
}
else if(a == 1){
dfs_used(h[0], v, g, used);
dfs_used(h[1], v, g, used);
return v;
}
else {//h1
dfs_used(v, h[1], g, used);
return h[1];
}
}
}
ll solve2(int v, vector<vector<int>> &g, vector<int> &sz, vector<int> &used){
ll nsz = dfs_sz(v, -1, g, sz, used);
P d = dfs_search(v, -1, nsz, g, sz, used);
ll nx = query(d, g, sz, used);
if(nx < 0) return nx + 1000000;
return solve2(nx, g, sz, used);
}
void solve(ll n){
vector<vector<int>> g(n + 10);
vector<int> sz(n + 10), used(n + 10);
REP(i, n){
ll a, b;
cin >> a >> b;
a--;
b--;
if(a != -1) {
g[i].push_back(a);
g[a].push_back(i);
}
if(b != -1) {
g[i].push_back(b);
g[b].push_back(i);
}
//ok
}
ll res = solve2(0, g, sz, used);
cout << "! " << res + 1 << endl;
}
int main() {
ll tt;
cin >> tt;
REP(i, tt){
ll n;
cin >> n;
solve(n);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3608kb
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: -100
Memory Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 0 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 1 2 5 3 0 5 1 0 0 0 0 4 0 2 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 0 10 2 8 9 7 0 0 ...
output:
? 2 8 ? 8 4 ? 8 5 ! 5 ? 3 7 ? 3 4 ? 2 1 ! 1 ? 1 8 ? 1 3 ? 1 5 ! 1 ? 2 5 ? 2 3 ! 3 ? 6 5 ? 3 8 ! 8 ? 5 1 ? 1 3 ! 1 ? 4 2 ? 5 1 ! 1 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 2 ? 2 7 ? 4 3 ? 6 5 ! 6 ? 1 2 ! 2 ? 5 9 ? 9 2 ? 2 6 ! 6 ? 5 10 ? 6 2 ? 4 10 ! 8 ? 3 4 ? 9 7 ? 9 8 ! 9 ? 1 2 ! 1 ? 3 4 ? 1 7 ! 7 ? 4 9 ? 2 8 ? ...