QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#761143 | #9570. Binary Tree | Meowmeowmeow# | WA | 7ms | 32780kb | C++20 | 4.0kb | 2024-11-18 21:10:17 | 2024-11-18 21:10:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 500005
#define pb push_back
int n,m;
set<int>v[MAXN];
int s[MAXN];
int c[MAXN],fa[MAXN];
int CNT = 0;
void dfs(int x,int ls) {
s[x] = 1;
CNT ++;
if(CNT > n) exit(0);
m ++;
c[m] = x;
for(auto y:v[x]) {
if(y != ls) {
fa[y] = x;
dfs(y,x);
s[x] += s[y];
}
}
}
signed main() {
int T;
cin >> T;
while(T --) {
for(int i = 0 ; i <= n; i ++) {
v[i].clear();
v[i].erase(v[i].begin(),v[i].end());
s[i] = 0; c[i] = 0; fa[i] = 0;
}
cin >> n;
int pp = 0;
for(; (1<<pp) <= n; pp ++) ;
for(int i = 1; i <= n; i ++) {
int x,y;
cin >> x >> y;
if(x != 0) {
v[i].insert(x);
v[x].insert(i);
}
if(y != 0) {
v[i].insert(y);
v[y].insert(i);
}
}
cout<<"? 1 2\n";
fflush(stdout);
return 0;
int o = 1;
int cnt = 0;
while(1) {
if(o > n) return 0;
m = 0;
fa[o] = 0;
CNT = 0;
dfs(o,0);
if(s[o] == 1) {
cout<<"! "<<o<<"\n";
cout.flush();
break;
}
int ss = s[o];
int t = 0,ux = 0,uy = 0;
for(int i = 1; i <= m; i ++) {
int x = c[i],mx = 0;
if(x > n) return 0;
for(auto y:v[x]) {
if(y > n) return 0;
if(fa[y] == x) mx = max(mx,s[y]);
else {
mx = max(mx,ss-s[x]);
}
}
if(mx <= m/2) {
t = x;
//cout<<t<<" "<<mx<<" "<<ss<<"?\n";
if(v[x].size() == 1) {
ux = *v[x].begin();
uy = t;
} else {
int wp[5],nt = 0;
for(auto y:v[x]) {
int sy = 0;
if(fa[y] == x) sy = s[y];
else sy = ss-s[y];
nt ++;
wp[nt] = sy*1000000+y;
}
sort(wp+1,wp+nt+1);
ux = wp[nt]%1000000;
uy = wp[nt-1]%1000000;
}
break;
}
}
if(t == 0 || t > n) return 0;
if(ux > n) return 0;
if(v[t].size() == 1) {
cnt ++;
if(cnt > pp) {
return 0;
}
cout<<"? "<<t<<" "<<ux<<"\n";
cout.flush();
int ans;
cin >> ans;
if(ans == 0) {
cout<<"! "<<t<<"\n";
cout.flush();
break;
} else {
cout<<"! "<<ux<<"\n";
cout.flush();
break;
}
}
if(ux <= 0 || uy <= 0 || ux > n || uy > n) return 0;
cnt ++;
if(cnt > pp) {
cout<<"wtf\n";
return 0;
}
cout<<"? "<<ux<<" "<<uy<<"\n";
cout.flush();
int ans;
cin >> ans;
if(ans == 0) {
v[ux].erase(t);
o = ux;
}
if(ans == 2) {
v[uy].erase(t);
o = uy;
}
if(ans == 1) {
v[t].erase(ux);
v[t].erase(uy);
o = t;
}
}
//cout<<o<<"\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 32780kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2
output:
? 1 2
result:
wrong answer format Unexpected end of file - token expected (test case 1)