QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#735938 | #9570. Binary Tree | paoxiaomo# | TL | 0ms | 5848kb | C++20 | 3.3kb | 2024-11-11 22:56:52 | 2024-11-11 22:56:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
// #define max(a, b) ((a) > (b) ? (a) : (b))
// #define min(a, b) ((a) < (b) ? (a) : (b))
#define pb push_back
#define LNF 1e18
#define INF 0x7fffffff
#define int long long
#define lowbit(x) ((x) & (-x))
#define abs(x) llabs(x)
// #define endl '\n'
#define Y() cout << "Yes" << endl
#define N() cout << "No" << endl
const db eps = 1e-9;
const int mod = 998244353;
const int MAXN = 2e5 + 5;
vector<pair<int, int>> pth[200005];
int siz[200005], maxp[200005], vis[200005], root, ans = 0, n, t;
void get_root(int pos, int fa, int total)
{
siz[pos] = 1;
maxp[pos] = 0;
for (auto &[to, w] : pth[pos])
{
if (to == fa || vis[to])
continue;
get_root(to, pos, total);
siz[pos] += siz[to];
maxp[pos] = max(maxp[pos], siz[to]);
}
maxp[pos] = max(maxp[pos], total - siz[pos]);
if (!root || maxp[pos] < maxp[root])
root = pos;
}
int calc(int pos)
{
int good = 0;
vector<int> nu;
for (auto &[to, w] : pth[pos])
{
if (!vis[to])
{
good++;
nu.push_back(to);
}
}
// cout << pos << " " << good << endl;
if (good == 0)
{
cout << "! " << pos << endl;
return -1;
}
else if (good == 1)
{
cout << "? " << nu[0] << " " << pos << endl;
cin >> t;
if (t == 0)
cout << "! " << nu[0] << endl;
else
cout << "! " << pos << endl;
return -1;
}
else if (good == 2)
{
cout << "? " << nu[0] << " " << nu[1] << endl;
cin >> t;
if (t == 1)
{
cout << "! " << pos << endl;
return -1;
}
else if (t == 0)
{
return nu[0];
}
else
return nu[1];
}
else
{
cout << "? " << nu[0] << " " << nu[1] << endl;
cin >> t;
if (t == 1)
{
return nu[2];
}
else if (t == 0)
{
return nu[0];
}
else
return nu[1];
}
}
int cnt=0;
void sol(int pos)
{
cnt++;
if(cnt>__lg(n))return;
int nu = calc(pos);
if (nu == -1)
return;
if (t == 1)
{
for (auto &[to, w] : pth[pos])
{
if (to != nu)
vis[to] = 1;
}
maxp[root = 0] = n;
get_root(pos, 0, siz[nu] + 1);
sol(root);
}
else
{
vis[pos] = 1;
maxp[root = 0] = n;
get_root(nu, 0, siz[nu]);
sol(root);
}
}
void add(int a, int b)
{
pth[a].push_back({b, 1});
pth[b].push_back({a, 1});
}
void solve()
{
cin >> n;cnt=0;
for (int i = 1; i <= n; i++)
{
int a, b;
cin >> a >> b;
if (a)
add(a, i);
if (b)
add(b, i);
}
maxp[root = 0] = n;
get_root(1, 0, n);
sol(root);
for (int i = 1; i <= n; i++){
pth[i].clear();
vis[i]=0;
}
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5848kb
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
Time Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 0 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 2 2 5 4 5 3 1 0 0 0 0 0 0 1 0
output:
? 2 5 ? 1 6 ? 6 7 ! 6 ? 5 3 ? 1 4 ? 3 2 ! 2 ? 1 6 ? 5 3 ? 3 7 ! 7 ? 4 5 ? 3 1