QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#730515 | #9570. Binary Tree | ucup-team4967# | RE | 1ms | 3620kb | C++20 | 3.1kb | 2024-11-09 20:32:13 | 2024-11-09 20:32:14 |
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>
vector<vector<int>> g;
vector<int> sz, used;
void dfs_sz(int nv, int np) {
stack<P> st;
st.push(P(nv - 1000000, np));
st.push(P(nv, np));
while(!st.empty()){
P tp = st.top();
st.pop();
ll v = tp.first, p = tp.second;
if(v >= 0){
for(auto& i: g[v]) {
if(used[i] or i == p)continue;
st.push(P(i - 1000000, v));
st.push(P(i, v));
}
}
else{
v += 1000000;
sz[v] = 1;
for(auto& i: g[v]) {
if(used[i] or i == p)continue;
sz[v] += sz[i];
}
}
}
}
P dfs_search(int nv, int np, int nsz){
stack<P> st;
st.push(P(nv, np));
while(!st.empty()){
ll now = -1;
P tp = st.top();
st.pop();
ll v = tp.first, p = tp.second;
bool ok = false;
for(auto& i: g[v]) {
if(used[i] or i == p)continue;
if(sz[i] > nsz / 2){
ok = true;
st.push(P(i, v));
}
if(sz[i] * 2 == nsz) now = i;
}
if(!ok){
return P(v, now);
}
}
return P(nv, -1);
}
void dfs_used(int nv, int np){
stack<P> st;
st.push(P(nv, np));
while(!st.empty()){
P tp = st.top();
st.pop();
ll v = tp.first, p = tp.second;
used[v] = true;
for(auto& i: g[v]) {
if(i == p or used[i])continue;
st.push(P(i, v));
}
}
}
ll query(P p){
// 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);
return p.first;
}
else {
dfs_used(p.first, p.second);
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]));
}
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]);
return h[0];
}
else if(a == 1){
dfs_used(h[0], v);
dfs_used(h[1], v);
return v;
}
else {//h1
dfs_used(v, h[1]);
return h[1];
}
}
}
ll solve2(int v){
dfs_sz(v, -1);
ll nsz = sz[v];
P d = dfs_search(v, -1, nsz);
ll nx = query(d);
if(nx < 0) return nx + 1000000;
return solve2(nx);
}
void solve(ll n){
g.resize(0);
sz.resize(0);
used.resize(0);
g.resize(n + 10);
sz.resize(n + 10);
used.resize(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);
cout << "! " << res + 1 << endl;
}
int main() {
ll tt;
cin >> tt;
REP(i, tt){
ll n;
cin >> n;
solve(n);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3620kb
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 ? 1 2 ! 2
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 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 1 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 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 ? 1 4 ? 2 3 ! 3 ? 1 8 ? 1 3 ? 1 5 ! 1 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 1 3 ! 1 ? 1 2 ? 3 5 ! 5 ? 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 ? 1 7 ? 8 2 ! 8 ? 1 2 ! 1 ? 4 3 ? 1 7 ! 4 ? 4 9 ? 2 8 ? ...