QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733899 | #9570. Binary Tree | AzusidNya | WA | 6ms | 6424kb | C++17 | 2.6kb | 2024-11-10 21:53:46 | 2024-11-10 21:53:46 |
Judging History
answer
#include<bits/stdc++.h>
// #define int long long
#define eb emplace_back
#define mp make_pair
#define DEBUG
using namespace std;
using pii = pair<int, int>;
using piii = pair<int, pii>;
using piiii = pair<pii, pii>;
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned int;
const int P = 998244353;const i64 inf = 0x3f3f3f3f3f3f3f3f;
namespace azus{
int n;
vector<int> edge[100005];
int siz[100005];bool vis[100005];
int rt, cty = 0;
int getroot(int u, int fa, int S){
siz[u] = 1;
int mx = 0;
for(int v : edge[u]){
if(vis[v] || v == fa) continue;
getroot(v, u, S); siz[u] += siz[v];
mx = max(mx, siz[v]);
}
mx = min(mx, S - siz[u]);
if(mx >= cty) rt = u, cty = mx;
return 0;
}
int dfs(int u, int fa){
siz[u] = 1;
for(int v : edge[u]){
if(vis[v] || v == fa) continue;
dfs(v, u); siz[u] += siz[v];
}
return 0;
}
int print(int u){
cout << "! " << u << "\n";
cout.flush();
return 0;
}
int dfz(int x, int S){
rt = 0, cty = 0;
getroot(x, 0, S);
int u = rt; vis[u] = 1;
dfs(u, 0);
int du = 0;
for(int v : edge[u]) du += (!vis[v]);
if(du == 0) return print(u), 0;
else if(du == 1){
int c = 0; for(int v : edge[u]) if(!vis[v]) c = v;
cout << "? " << u << " " << c << "\n";
cout.flush(); int x;
cin >> x; if(x == 0) return print(u), 0;
else return print(c), 0;
} else if(du == 2){
vector<int> c;
for(int v : edge[u]) if(!vis[v]) c.eb(v);
cout << "? " << c[0] << " " << c[1] << "\n";
cout.flush(); int x;
cin >> x; if(x == 1) return print(u), 0;
else if(x == 0) return dfz(c[0], siz[c[0]]), 0;
else return dfz(c[1], siz[c[1]]), 0;
} else{
vector<int> c;
for(int v : edge[u]) if(!vis[v]) c.eb(v);
sort(c.begin(), c.end(), [&](int x, int y){return siz[x] > siz[y];});
cout << "? " << c[0] << " " << c[1] << "\n";
cout.flush(); int x;
cin >> x; if(x == 1){
vis[c[0]] = 1, vis[c[1]] = 1, vis[u] = 0;
return dfz(u, S - siz[c[0]] - siz[c[1]]), 0;
}
else if(x == 0) return dfz(c[0], siz[c[0]]), 0;
else return dfz(c[1], siz[c[1]]), 0;
}
return 0;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i ++)
edge[i].clear(), siz[i] = 0, vis[i] = 0;
for(int u = 1; u <= n; u ++){
vector<int> c;
for(int i = 1; i <= 2; i ++){
int x; cin >> x; c.eb(x);
}
for(int v : c) if(v != 0)
edge[u].eb(v), edge[v].eb(u);
}
dfz(1, n);
return 0;
}
}
signed main(){
#ifndef DEBUG
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#endif
int T = 1;
cin >> T;
while(T --)
azus::main();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 6184kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 2 5 ! 2 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 6ms
memory: 6424kb
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 2 2 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 2 0 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 0 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 2 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 4 ? 2 7 ? 2 1 ! 1 ? 2 7 ? 7 8 ? 8 6 ! 6 ? 1 6 ? 1 7 ? 1 5 ! 1 ? 3 1 ? 4 5 ! 4 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 5 4 ! 5 ? 4 1 ? 5 2 ! 2 ? 3 2 ! 2 ? 1 2 ! 2 ? 2 3 ! 3 ? 2 6 ? 2 10 ? 2 8 ! 2 ? 1 2 ! 1 ? 5 9 ? 6 9 ? 9 1 ! 9 ? 3 10 ? 4 10 ? 6 2 ! 2 ? 8 2 ? 8 6 ? 3 5 ! 5 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 4 ? 6 2 ? 7 6 ?...
result:
wrong answer Too many queries (test case 33)