QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#728157 | #9570. Binary Tree | ucup-team3802# | RE | 1ms | 3596kb | C++23 | 3.0kb | 2024-11-09 14:40:20 | 2024-11-09 14:40:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = a; i < b; i++)
void solve(){
int n;
cin >> n;
vector<vector<int>> g(n);
rep(i, 0, n){
int a,b;
cin >> a >> b;
a--,b--;
if(a != -1){
g[i].emplace_back(a);
g[a].emplace_back(i);
}
if(b != -1){
g[i].emplace_back(b);
g[b].emplace_back(i);
}
}
vector<int> none(n, 0);
int ex = n;
auto deldfs = [&](auto self, int nw, int p)-> void {
ex--;
none[nw] = 1;
for(auto to: g[nw]){
if(to == p) continue;
self(self, to, nw);
}
};
while(ex > 1){
if(ex == 2){
vector<int> d;
rep(i,0,n) if(none[i] == 0) d.push_back(i);
assert(ex == 2);
cout << "? " << d[0] + 1 << " " << d[1] + 1 << endl;
int ch;
cin >> ch;
if(ch == 0){
none[d[1]] = 1;
}else{
none[d[0]] = 1;
}
break;
}
priority_queue<pair<int,int>> q;
vector<int> out(n, 0);
vector<int> v(n, 1);
rep(i, 0, n){
if(none[i]) out[i] = 0;
else{
int cnt = 0;
for(auto to: g[i]){
if(none[to]) continue;
cnt++;
}
if(cnt == 1) q.push({-1, i});
else out[i] = cnt;
}
}
while(q.size()){
auto [_v, nw] = q.top();
q.pop();
for(auto to: g[nw]){
if(none[to]) continue;
out[to]--;
if(out[to] < 0) continue;
v[to] += v[nw];
if(out[to] == 0){
q.push({-v[to], to});
}
}
}
int mx = 0;
int mid = -1;
rep(i,0,n){
if(none[i]) continue;
if(mx < v[i]) {
mx = v[i];
mid = i;
}
}
int U =-1,V =-1;
for(auto to: g[mid]){
if(U == -1) U = to;
else if(V == -1) V = to;
else{
if(v[U] <= v[V] && v[U] < v[to]) U = to;
else if(v[V] <= v[U] && v[V] < v[to]) V = to;
}
}
cout << "? " << U + 1 << " " << V + 1 << endl;
int ch;
cin >> ch;
if(ch == 0){
deldfs(deldfs, mid, U);
}
if(ch == 1){
deldfs(deldfs, U, mid);
deldfs(deldfs, V, mid);
}
if(ch == 2){
deldfs(deldfs, mid, V);
}
}
int ls = 0;
rep(i,0,n) if(none[i] == 0) ls = i+1;
cout << "! " << ls << endl;
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3596kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 1 2 ! 1 ? 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 2 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 1 2 0
output:
? 6 8 ? 3 8 ? 5 8 ! 8 ? 1 4 ? 2 7 ? 5 6 ! 8