QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#742391 | #9570. Binary Tree | 2317663977 | TL | 1ms | 3788kb | C++23 | 3.3kb | 2024-11-13 16:31:11 | 2024-11-13 16:31:16 |
Judging History
answer
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstring>
using ll = long long;
const int N = 2e5 + 10;
int n;
vector<int> v[N];
bool st[N];
int sz[N], weight[N];
int dfs(int u, int fa)
{
sz[u] = 1;
weight[u] = 0;
for (auto it : v[u])
{
if (it == fa) continue;
if (st[it]) continue;
int ans = dfs(it, u);
if (ans != 0) return ans;
sz[u] += sz[it];
weight[u] = max(weight[u], sz[it]);
}
weight[u] = max(weight[u], n - sz[u]);
// cout << weight[u] << ' ' << u << '\n';
if (weight[u] <= n / 2) return u;
return 0;
}
int mk(int u, int fa)
{
st[u] = true;
int res = 1;
for (auto it : v[u])
{
if (it == fa) continue;
if (st[it]) continue;
res += mk(it, u);
}
return res;
}
int find(int u, int fa)
{
int res = 1;
//cout << u << ' ' << fa << '\n';
for (auto it : v[u])
{
if (it == fa) continue;
if (st[it]) continue;
res += find(it, u);
}
return res;
}
void solve()
{
cin >> n;
for (int i = 1;i <= n;i++) v[i].clear();
fill(st, st + 1 + n, 0);
for (int i = 1;i <= n;i++)
{
int a, b;
cin >> a >> b;
if (a != 0) v[i].push_back(a), v[a].push_back(i);
if (b != 0) v[i].push_back(b), v[b].push_back(i);
}
while (true)
{
fill(sz, sz + 1 + n, 0);
fill(weight, weight + 1 + n, 0);
int root = 1;
while (st[root]) root++;
root = dfs(root, -1);
vector<int> ne;
for (auto it : v[root])
if (!st[it])
ne.push_back(it);
if (ne.size() == 0)
{
cout << "! " << root << '\n';
cout.flush();
return;
}
else if (ne.size() == 1)
{
cout << "? " << root << ' ' << ne[0] << '\n';
cout.flush();
int ans;
cin >> ans;
if (ans == 0) n -= mk(ne[0], root);
else if (ans == 2) n -= mk(root, ne[0]);
}
else if (ne.size() == 2)
{
cout << "? " << ne[0] << ' ' << ne[1] << '\n';
cout.flush();
int ans;
cin >> ans;
if (ans == 0) n -= mk(root, ne[0]);
else if (ans == 2) n -= mk(root, ne[1]);
else
{
cout << "! " << root << '\n';
cout.flush();
return;
}
}
else
{
int a = find(ne[0], root);
int b = find(ne[1], root);
int c = find(ne[2], root);
if (c < a && c < b)
{
cout << "? " << ne[0] << ' ' << ne[1] << '\n';
cout.flush();
int ans;
cin >> ans;
if (ans == 0) n -= mk(root, ne[0]);
else if (ans == 2) n -= mk(root, ne[1]);
else
{
n -= mk(ne[0], root);
n -= mk(ne[1], root);
}
}
else if (b < a && b < c)
{
cout << "? " << ne[0] << ' ' << ne[2] << '\n';
cout.flush();
int ans;
cin >> ans;
if (ans == 0) n -= mk(root, ne[0]);
else if (ans == 2) n -= mk(root, ne[2]);
else
{
n -= mk(ne[0], root);
n -= mk(ne[2], root);
}
}
else
{
cout << "? " << ne[1] << ' ' << ne[2] << '\n';
cout.flush();
int ans;
cin >> ans;
if (ans == 0) n -= mk(root, ne[1]);
else if (ans == 2) n -= mk(root, ne[2]);
else
{
n -= mk(ne[1], root);
n -= mk(ne[2], root);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int T = 1;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3788kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 5 3 ? 4 3 ! 4 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Time Limit Exceeded
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 1 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 2 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 1 1 0 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 0 2 2 0 0 0 2 3 2 3 0 0 0 0 1 10 2 8 9 7 0 ...
output:
? 2 4 ? 2 7 ? 2 1 ! 1 ? 5 3 ? 4 3 ? 2 1 ! 2 ? 1 6 ? 1 7 ? 5 1 ! 1 ? 5 2 ? 4 1 ! 1 ? 6 7 ? 1 4 ? 5 3 ! 5 ? 5 1 ? 5 4 ! 5 ? 2 4 ? 5 1 ! 1 ? 3 2 ! 3 ? 2 1 ! 1 ? 2 3 ! 1 ? 2 6 ? 1 9 ? 10 9 ! 9 ? 2 1 ! 1 ? 5 9 ? 6 9 ? 9 1 ! 9 ? 5 8 ? 1 5 ? 3 9 ! 9 ? 3 9 ? 7 9 ? 2 1 ! 2 ? 2 1 ! 2 ? 4 3 ? 1 7 ! 7 ? 9 5 ? 8...