QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733435 | #9570. Binary Tree | hhdhh | WA | 2ms | 5668kb | C++23 | 2.9kb | 2024-11-10 18:55:29 | 2024-11-10 18:55:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
// #define endl '\n'
#define debug(x) cout<<#x<<": "<<x<<endl
const int N=2e5+10;
typedef pair<ll,ll>PLL;
int num=0;
ll n;
int ask(int x, int y)
{
if(++num>__lg(n)) exit(0);
cout << "? " << x << ' ' << y << endl;
cout.flush();
cin >> x;
return x;
}
void answer(int x)
{
cout << "! " << x << endl;
cout.flush();
}
vector<int>e[N];
int siz[N],del[N];
int sum,root,mxs;
int getsum(int u,int fa)
{
siz[u]=1;
for(auto v:e[u])
{
if(v==fa||del[v]) continue;
getsum(v,u);
siz[u]+=siz[v];
}
return siz[u];
}
int su[N];
void getroot(int u,int fa)
{
siz[u]=1;
int s=0;
for(auto v:e[u])
{
if(v==fa||del[v]) continue;
getroot(v,u);
siz[u]+=siz[v];
s=max(s,siz[v]);
}
s=max(s,sum-siz[u]);
su[u]=s;
if(s<mxs) mxs=s,root=u;
}
void init(int u)
{
sum=getsum(u,0);
mxs=1000000000;
}
void solve()
{
num=0;
cin >> n;
for(int i=1;i<=n;i++) e[i].clear(),del[i]=0,siz[i]=0;
sum=root=mxs=0;
rep(i, 1, n)
{
int u, v;
cin >> u >> v;
if(u!=0)
{
e[i].push_back(u);
e[u].push_back(i);
}
if(v!=0)
{
e[i].push_back(v);
e[v].push_back(i);
}
}
root=1;
while(1)
{
init(root);
getroot(root,0);
int x=0,y=0;
for(auto p:e[root])
{
if(!del[p])
{
if(su[y]<su[p])
y=p;
if(su[x]<su[y])
swap(x,y);
}
}
// cout.flush();
if(x==0)
{
answer(root);
return;
}
if(y==0)
{
int ans=ask(x,root);
if(ans==0)
{
answer(x);
return;
}
if(ans == 2)
{
answer(root);
return;
}
}
int ans=ask(x,y);
if(ans==0)
{
del[root]=1;
del[y]=1;
root=x;
}
if(ans==1)
{
del[x]=1;
del[y]=1;
root=root;
}
if(ans==2)
{
del[x]=1;
del[root]=1;
root=y;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
// #ifndef ONLINEJUDGE
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
// #endif
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 5668kb
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
Wrong Answer
time: 0ms
memory: 3632kb
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 1 1 1 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 2 2 2 5 4 5 3 1 0 0 0 0 0 0 1 0 8 0 0 0 0 5 6 0 0 1 4 2 0 3 8 0 0 1 1 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 1 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 ...
output:
? 5 4 ? 1 8 ? 2 7 ! 6 ? 5 3 ? 1 4 ? 2 3 ! 3 ? 6 1 ? 7 1 ? 1 5 ! 5 ? 4 5 ? 3 1 ! 3 ? 6 7 ? 3 1 ? 5 4 ! 5 ? 5 1 ? 5 4 ! 4 ? 1 2 ? 3 5 ! 3 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 5 6 ? 7 1 ? 2 10 ! 10 ? 1 2 ! 1 ? 5 9 ? 4 8 ? 3 5 ! 3 ? 8 5 ? 1 5 ? 3 9 ! 7 ? 4 3 ? 1 7 ? 8 2 ! 2 ? 1 2 ! 2 ? 6 3 ? 5 1
result:
wrong answer format Unexpected end of file - token expected (test case 17)