QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#729619 | #9570. Binary Tree | ucup-team191# | RE | 1ms | 5760kb | C++23 | 1.9kb | 2024-11-09 17:30:04 | 2024-11-09 17:30:08 |
Judging History
answer
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)
const int N=300010,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;
int t,n,ss[N],par[N];
vi ch[N],ord;
int ask(int a,int b)
{
cout<<"? "<<a<<' '<<b<<endl;
int x;
cin>>x;
return x;
}
void dfs(int i,int p=-1)
{
ss[i]=1;
ord.pb(i);
par[i]=p;
for (auto x: ch[i]) if (x!=p)
{
dfs(x,i);
ss[i]+=ss[x];
}
}
void rem(int a,int b)
{
ch[a].erase(find(all(ch[a]),b));
ch[b].erase(find(all(ch[b]),a));
}
int solve(int r)
{
ord.clear();
dfs(r);
if (ss[r]==1)
{
cout<<"! "<<r<<endl;
return -1;
}
if (ss[r]==2)
{
int b=ch[r][0];
int re=ask(r,b);
rem(r,b);
if (re==0) return r;
if (re==2) return b;
assert(0);
}
int cn=ss[r],cen=-1;
for (auto x: ord)
{
if (cn-ss[x]>cn/2) continue;
bool ok=1;
for (auto y: ch[x]) if (y!=par[x] && ss[y]>cn/2) ok=0;
if (ok) cen=x;
}
assert(cen!=-1);
if (ch[cen].size()==2)
{
int a=ch[cen][0],b=ch[cen][1];
rem(cen,a);
rem(cen,b);
int re=ask(a,b);
if (re==0) return a;
if (re==2) return b;
if (re==1) return cen;
}
int mal=-1;
for (auto x: ch[cen]) if (ss[x]<cn) mal=x;
assert(mal!=-1);
int a=-1,b=-1;
for (auto x: ch[cen]) if (x!=mal)
{
if (a==-1) a=x;
else b=x;
}
rem(cen,a);
rem(cen,b);
//rem(cen,mal);
int re=ask(a,b);
if (re==0) return a;
if (re==1) return mal;
if (re==2) return b;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>t;
while (t--)
{
cin>>n;
for (int i=1;i<=n;++i) ch[i].clear();
for (int i=1;i<=n;++i)
{
int x,y;
cin>>x>>y;
if (x) ch[x].pb(i),ch[i].pb(x);
if (y) ch[y].pb(i),ch[i].pb(y);
}
int cu=1;
while (cu!=-1) cu=solve(cu);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5760kb
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
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 1 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 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 0 ...
output:
? 2 5 ? 2 7 ? 2 1 ! 1 ? 5 3 ? 1 3 ? 4 2 ! 4 ? 1 6 ? 1 7 ? 1 5 ! 5 ? 4 5 ? 3 1 ! 1 ? 5 6 ? 1 4 ! 1 ? 5 1 ? 5 4 ! 4 ? 1 2 ? 3 5 ! 3 ? 3 2 ! 2 ? 1 2 ! 1 ? 2 3 ! 3 ? 2 6 ? 1 9 ? 9 10 ! 10 ? 1 2 ! 1 ? 5 9 ? 4 5 ? 8 3 ! 8 ? 5 8 ? 7 5 ? 1 3 ! 1 ? 3 4 ? 1 9 ? 7 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? ...