QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#763656 | #9570. Binary Tree | zhuge0 | RE | 0ms | 12076kb | C++17 | 3.1kb | 2024-11-19 21:31:25 | 2024-11-19 21:31:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 200005
ll n,m,k,w,g[2],all=0;
string s;
vector<ll> e[MAXN];
ll sz[MAXN], weight[MAXN];
void dfs(ll u,ll fa)
{
sz[u] = 1;
weight[u] = 0;
for (auto v : e[u])
{
if (v == fa) continue;
dfs(v,u);
sz[u] += sz[v];
weight[u] = max(weight[u],sz[v]);
}
weight[u] = max(weight[u],all-sz[u]);
if (weight[u] <= all / 2)
{
g[g[0] != 0] = u;
}
}
ll siz[MAXN];
void dfs_2(int u,int fa)
{
siz[u] = 1;
for (auto v : e[u])
{
if (v == fa) continue;
dfs_2(v, u);
siz[u] += siz[v];
}
}
int ask(int a,int b)
{
int x;
cout << "? " << a << ' ' << b << endl;
cin >> x;
return x;
}
void solve()
{
cin >> n;
for (int i = 1; i <= n; i++) e[i].clear();
for (int i = 1; i <= n; i++)
{
int u, v;
cin >> u >> v;
if (u != 0) e[u].push_back(i), e[i].push_back(u);
if (v != 0) e[v].push_back(i), e[i].push_back(v);
}
g[0] = 0, g[1] = 0;
dfs_2(1, 0);
all = siz[1];
dfs(1,0);
while (e[g[0]].size()!=0)
{
if (e[g[0]].size() == 0)
{
break;
}
else if (e[g[0]].size() == 1)
{
int tag=ask(e[g[0]][0],g[0]);
if (tag == 0)
{
swap(e[g[0]][0],g[0]);
}
break;
}
else if(e[g[0]].size()==2)
{
int tag=ask(e[g[0]][0],e[g[0]][1]);
if (tag == 0)
{
int cnt = 0;
for (auto i : e[e[g[0]][0]])
{
if (i == g[0]) break;
cnt++;
}
e[e[g[0]][0]].erase(e[e[g[0]][0]].begin()+cnt);
int now = e[g[0]][0];
g[0] = 0, g[1] = 0;
dfs_2(now, 0);
all = siz[now];
dfs(now, 0);
}
else if(tag==1)
{
break;
}
else
{
int cnt = 0;
for (auto i : e[e[g[0]][1]])
{
if (i == g[0]) break;
cnt++;
}
e[e[g[0]][1]].erase(e[e[g[0]][1]].begin() + cnt);
int now = e[g[0]][1];
g[0] = 0, g[1] = 0;
dfs_2(now, 0);
all = siz[now];
dfs(now, 0);
}
}
else
{
dfs_2(g[0], 0);
sort(e[g[0]].begin(), e[g[0]].end(), [&](int x, int y)
{
return siz[x] > siz[y];
});
int tag=ask(e[g[0]][0],e[g[0]][1]);
if (tag == 0)
{
int cnt = 0;
for (auto i : e[e[g[0]][0]])
{
if (i == g[0]) break;
cnt++;
}
e[e[g[0]][0]].erase(e[e[g[0]][0]].begin() + cnt);
int now = e[g[0]][0];
g[0] = 0, g[1] = 0;
dfs_2(now, 0);
all = siz[now];
dfs(now, 0);
}
else if(tag==1)
{
e[g[0]].erase(e[g[0]].begin());
e[g[0]].erase(e[g[0]].begin());
int now = e[g[0]][0];
g[0] = 0, g[1] = 0;
dfs_2(now, 0);
all = siz[now];
dfs(now, 0);
//cout << e[now].size() << endl;
}
else
{
int cnt = 0;
for (auto i : e[e[g[0]][2]])
{
if (i == g[0]) break;
cnt++;
}
e[e[g[0]][2]].erase(e[e[g[0]][2]].begin() + cnt);
int now = e[g[0]][2];
g[0] = 0, g[1] = 0;
dfs_2(now, 0);
all = siz[now];
dfs(now, 0);
}
}
}
cout << "! " << g[0] << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
cin >> T;
while (T--)
{
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 12076kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 1 ? 5 2 ! 5 ? 1 2 ! 2
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 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 0 2 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 0 0 5 3 0 5 1 0 0 0 0 4 0 0 2 5 5 0 0 0 0 0 3 0 2 4 0 0 3 3 0 1 0 0 0 2 2 2 0 0 0 0 3 2 3 0 0 0 0 2 10 2 8 9 7 0 0 ...
output:
? 2 4 ? 2 7 ? 2 1 ! 1 ? 5 3 ? 1 4 ? 3 2 ! 3 ? 1 6 ? 1 7 ? 1 5 ! 5 ? 2 4 ? 2 3 ! 3 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 5 4 ! 4 ? 4 1 ? 4 3 ! 4 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 9 10 ! 10 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 5 3 ! 5 ? 5 8 ? 7 1 ? 5 3 ! 5 ? 9 3 ? 1 7 ? 9 2 ! 9 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? ...