QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#768975 | #9570. Binary Tree | FZY0314 | WA | 4ms | 3600kb | C++20 | 2.9kb | 2024-11-21 15:33:10 | 2024-11-21 15:33:18 |
Judging History
answer
#include<iostream>
#include<queue>
#include<cstring>
#include<tuple>
//~FZY.//
#include<bits/stdc++.h>
#define Heap_int priority_queue<int, vector<int>, greater<int>>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define out(x) cout << ((x) ? "YES" : "NO") << endl
#define mod(x, P) (((x) % (P) + (P)) % (P))
//#define mod(x) (((x) % (P) + (P)) % (P))
#define ULL unsigned long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define lowbit(x) ((x) & -(x))
#define LL long long
#define pb push_back
#define gcd __gcd
#define Big __int128
//#define endl "\n"
#define x first
#define y second
//#define int LL
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10, INF = 1e9 + 7, P = 998244353;
vector<int>a[N];
int g[N],dp[N],hd,sum,mi=P;
void as(int u,int fa)
{
sum++;
for(auto i:a[u])
{
if(i!=fa&&!g[i])
as(i,u);
}
}
void dfs(int u,int fa)
{
dp[u]=1;
int ma=0;
for(auto i:a[u])
{
if(i!=fa&&!g[i])
{
dfs(i,u);
dp[u]+=dp[i];
ma=max(ma,dp[i]);
}
}
ma=max(ma,sum-dp[u]);
if(ma<mi)
mi=ma,hd=u;
}
void dfs1(int u)
{
mi=P,sum=0;
as(u,0);
dfs(u,0);
vector<int>q;
for(auto i:a[hd])
{
if(!g[i])
q.push_back(i);
}
if(!q.size())
cout<<"!"<<" "<<hd<<endl;
else if(q.size()==1)
{
cout<<"?"<<" "<<q[0]<<" "<<hd<<endl;
int x;
cin>>x;
if(x==0)
cout<<"!"<<" "<<q[0]<<endl;
else
cout<<"!"<<" "<<hd<<endl;
return;
}
else if(q.size()==2)
{
cout<<"?"<<" "<<q[0]<<" "<<q[1]<<endl;
int x;
cin>>x;
if(x==0)
{
g[hd]++;
dfs1(q[0]);
}
else if(x==1)
{
cout<<"!"<<" "<<hd<<endl;
return;
}
else
{
g[hd]++;
dfs1(q[1]);
}
}
else
{
cout<<"?"<<" "<<q[0]<<" "<<q[1]<<endl;
int x;
cin>>x;
if(x==0)
{
g[hd]++;
dfs1(q[0]);
}
else if(x==1)
{
g[q[0]]++,g[q[1]]++;
dfs1(hd);
}
else
{
g[hd]++;
dfs1(q[1]);
}
}
}
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int u,v;
cin>>u>>v;
if(u)
{
a[u].push_back(i);
a[i].push_back(u);
}
if(v)
{
a[v].push_back(i);
a[i].push_back(v);
}
}
dfs1(1);
for(int i=1;i<=n;i++)
a[i].clear(),g[i]=0;
}
signed main(){
IOS;
int T = 1;
cin >> T;
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3484kb
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: 4ms
memory: 3600kb
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 4 ? 2 3 ! 2 ? 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 8 ? 3 5 ! 3 ? 5 8 ? 7 1 ? 3 5 ! 3 ? 3 4 ? 1 7 ? 8 2 ! 2 ? 1 2 ! 2 ? 4 3 ? 1 7 ! 7 ? 4 9 ? 2 3 ? ...
result:
wrong answer Too many queries (test case 90)