QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#735918 | #9570. Binary Tree | paoxiaomo# | WA | 0ms | 7724kb | C++20 | 3.3kb | 2024-11-11 22:41:38 | 2024-11-11 22:41:38 |
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[500005];
int siz[500005], maxp[500005], vis[500005], 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];
}
}
void sol(int pos)
{
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;
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();
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7724kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 1 2 0 2 0 0
output:
? 1 5 ? 2 4 ! 3 ! 2
result:
wrong answer There are 2 candidates. (test case 2)