QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#786661 | #9570. Binary Tree | Andyqian7 | WA | 1ms | 3660kb | C++20 | 3.7kb | 2024-11-26 22:43:18 | 2024-11-26 22:43:19 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, s, e) for (int i = s; i <= e; i++)
using namespace std;
const int N = 1e5 + 10;
int n, sz[N], rt, mi, vis[N], tot;
vector<int> g2[N], g3[N];
void dfs(int s, int fa)
{
sz[s] = 1;
int cur = 0;
for (int son : g2[s])
{
if (son == fa || vis[son])
continue;
dfs(son, s);
sz[s] += sz[son];
cur = max(cur, sz[son]);
}
cur = max(cur, tot - sz[s]);
if (cur < mi)
rt = s, mi = cur;
}
void dfs2(int s, int fa)
{
g3[s].clear();
for (int son : g2[s])
{
if (son == fa || vis[son])
continue;
g3[s].push_back(son);
dfs2(son, s);
}
}
int query(int u, int v)
{
cout << "? " << u << " " << v << endl;
int t;
cin >> t;
return t;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
int T;
cin >> T;
while (T--)
{
cin >> n;
tot = n;
rep(i, 1, n)
{
vis[i] = 0;
g2[i].clear();
}
rep(i, 1, n)
{
int x, y;
cin >> x >> y;
if (x)
g2[i].push_back(x), g2[x].push_back(i);
if (y)
g2[i].push_back(y), g2[y].push_back(i);
}
mi = 1e9;
dfs(1, 0), dfs2(rt, 0);
while (tot)
{
mi = 1e9;
vis[rt] = 1;
switch (g3[rt].size())
{
case 3:
rep(i, 0, 2)
{
rep(j, 0, 1)
{
if (sz[g3[rt][j]] < sz[g3[rt][j + 1]])
swap(g3[rt][j], g3[rt][j + 1]);
}
}
switch (query(g3[rt][0], g3[rt][1]))
{
case 0:
tot = sz[g3[rt][0]];
dfs(g3[rt][0], 0), dfs2(rt, 0);
break;
case 1:
vis[g3[rt][0]] = vis[g3[rt][1]] = 1;
vis[rt] = 0;
tot = sz[g3[rt][2]] + 1;
vis[rt] = 0;
dfs(rt, 0), dfs2(rt, 0);
break;
case 2:
tot = sz[g3[rt][1]];
dfs(g3[rt][1], 0), dfs2(rt, 0);
break;
default:
break;
}
break;
case 2:
switch (query(g3[rt][0], g3[rt][1]))
{
case 0:
tot = sz[g3[rt][0]];
dfs(g3[rt][0], 0), dfs2(rt, 0);
break;
case 1:
cout << "! " << rt << endl;
tot = 0;
break;
case 2:
tot = sz[g3[rt][1]];
dfs(g3[rt][1], 0), dfs2(rt, 0);
break;
default:
break;
}
break;
case 1:
switch (query(rt, g3[rt][0]))
{
case 0:
cout << "! " << rt << endl;
tot = 0;
break;
case 2:
cout << "! " << g3[rt][0] << endl;
tot = 0;
break;
default:
break;
}
break;
case 0:
cout << "! " << rt << endl;
tot = 0;
break;
default:
break;
}
}
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3632kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 4 3 ! 4 ? 2 1 ! 1
result:
ok OK (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3660kb
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 0 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 2 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 ? 1 6 ? 7 6 ! 7 ? 5 3 ? 3 1 ? 4 2 ! 2 ? 1 6 ? 5 3 ? 7 3 ! 3 ? 2 4 ? 3 2 ! 3 ? 5 6 ? 1 4 ! 4 ? 5 1 ? 4 5 ! 4 ? 1 4 ? 3 4 ! 4 ? 3 2 ! 2 ? 2 1 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 8 1 ! 8 ? 2 1 ! 2 ? 5 9 ? 2 1 ? 6 2 ! 6 ? 5 8 ? 5 7 ? 1 3 ! 3 ? 9 3 ? 9 1 ? 7 2 ! 2 ? 2 1 ! 1 ? 4 3 ? 1 7 ! 7 ? 9 4 ? 2 3 ? 2 ...
result:
wrong answer There are 2 candidates. (test case 20)