QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#722116 | #9570. Binary Tree | ucup-team4474# | RE | 1ms | 4012kb | C++20 | 2.3kb | 2024-11-07 17:54:04 | 2024-11-07 17:54:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
bool Memory_begin;
bool Memory_end;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cerr << (&Memory_end - &Memory_begin) / 1048576.0 << "MB" << '\n';
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
for (int i = 1, x, y; i <= n; i++)
{
cin >> x >> y;
if (x)
adj[i].push_back(x), adj[x].push_back(i);
if (y)
adj[i].push_back(y), adj[y].push_back(i);
}
vector<int> vis(n + 1);
int siz = n;
while (siz > 1)
{
vector<int> dep(n + 1);
auto dfs = [&](auto &dfs, int cur, int fa) -> void
{
dep[cur] = dep[fa] + 1;
for (int i : adj[cur])
{
if (vis[i] or i == fa)
continue;
dfs(dfs, i, cur);
}
};
for (int i = 1; i <= n; i++)
{
if (vis[i])
continue;
dfs(dfs, i, 0);
int p1 = max_element(dep.begin(), dep.end()) - dep.begin();
dfs(dfs, p1, 0);
auto dep1 = dep;
int p2 = max_element(dep.begin(), dep.end()) - dep.begin();
dfs(dfs, p2, 0);
auto dep2 = dep;
cout << "? " << p1 << ' ' << p2 << endl;
int tmp;
cin >> tmp;
for (int j = 1; j <= n; j++)
{
if (vis[j])
continue;
if (tmp == 0 and dep1[j] >= dep2[j])
vis[j] = 1, siz--;
else if (tmp == 1 and dep1[j] != dep2[j])
vis[j] = 1, siz--;
else if (tmp == 2 and dep1[j] <= dep2[j])
vis[j] = 1, siz--;
}
break;
}
}
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
cout << "! " << i << endl;
break;
}
}
}
}
/*
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4012kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 1 2 0 2 0 0 2
output:
? 4 1 ? 5 1 ! 2 ? 2 1 ! 1
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 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 2 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 2 0 5 3 0 5 1 0 0 0 0 4 0 2 2 5 5 0 0 0 0 0 3 0 2 4 2 2 3 3 0 1 0 0 0 2 2 2 0 0 0 2 3 2 3 0 0 0 0 0 10 2 8 9 7 0 0 ...
output:
? 3 7 ? 7 1 ? 7 6 ! 6 ? 6 1 ? 3 1 ? 4 2 ! 4 ? 2 7 ? 7 5 ? 7 3 ! 7 ? 3 4 ? 4 5 ! 4 ? 2 1 ? 4 1 ! 4 ? 4 3 ? 3 1 ! 1 ? 3 1 ? 2 1 ! 1 ? 2 3 ! 3 ? 2 1 ! 1 ? 2 3 ! 2 ? 3 8 ? 5 3 ? 4 3 ! 6 ? 2 1 ! 2 ? 4 6 ? 6 1 ? 9 1 ! 1 ? 2 9 ? 9 1 ? 5 1 ! 1 ? 5 1 ? 4 5 ? 5 6 ! 5 ? 2 1 ! 2 ? 2 1 ? 7 1 ! 4 ? 3 1 ? 7 1 ? 9 ...