QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#736335 | #9570. Binary Tree | Kokomi | ML | 0ms | 9724kb | C++23 | 4.6kb | 2024-11-12 10:09:48 | 2024-11-12 10:09:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<ll, ll> pll;
const int N = 2e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
ll n,m;
vector<ll>ve[N];
set<ll>trees;
int cen[5];
int cenpos;
ll dp[N][3],sum[N],minn[N],num;
void dfs(ll u,ll fa){
dp[u][0]=dp[u][1]=0;
sum[u]=1;
minn[u]=0;
int cur=0;
for(auto e:ve[u]){
if(e==fa)continue;
dfs(e,u);
dp[u][cur++]=sum[e];
sum[u]+=sum[e];
}
for(int i=0;i<cur;i++){
minn[u]=max(minn[u],dp[u][i]);
}
minn[u]=max(minn[u],num-sum[u]);
}
int ask(int u,int v){
cout<<"? "<<u<<' '<<v<<endl;
cout.flush();
int tag;cin>>tag;
return tag;
}
void inidfs(int u,int fa){
trees.insert(u);
for(auto e:ve[u]){
if(e==fa)continue;
inidfs(e,u);
}
}
void init(int u){
trees.clear();
inidfs(u,0);
}
ll dfs1(int u,int fa){
ll res=1;
for(auto e:ve[u]){
if(e==fa)continue;
res+=dfs1(e,u);
}
return res;
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++)ve[i].clear(),trees.insert(i);
for(int i=1;i<=n;i++){
int x,y;cin>>x>>y;
if(x){
ve[i].push_back(x);
ve[x].push_back(i);
}
if(y){
ve[i].push_back(y);
ve[y].push_back(i);
}
}
while(trees.size()>1){//logn
int root=*trees.begin();
num=trees.size();
cenpos=0;
dfs(root,0);
ll tmpmin=INF;
for(auto e:trees){
tmpmin=min(tmpmin,minn[e]);
}
for(auto e:trees){
if(minn[e]==tmpmin){
cen[cenpos++]=e;
}
}
if(cenpos==1){
int center=cen[0];
int lcen,rcen;
if(ve[center].size()==2){
lcen=ve[center][0];
rcen=ve[center][1];
}
else if(ve[center].size()==1){
lcen=ve[center][0];
rcen=center;
}
else{////////////////////////////
priority_queue<pll>pq;
for(auto e:ve[center]){
pq.push({dfs1(e,center),e});
}
lcen=pq.top().second;
pq.pop();
rcen=pq.top().second;
}
int tag=ask(lcen,rcen);
if(tag==0){
auto p=ve[lcen].begin();
for(;p!=ve[lcen].end();p++){
if(*p==center){
ve[lcen].erase(p);
break;
}
}
init(lcen);
}
else if(tag==1){
auto p=ve[center].begin();
for(;p!=ve[center].end();p++){
if(*p==lcen){
ve[center].erase(p);
break;
}
}
for(p=ve[center].begin();p!=ve[center].end();p++){
if(*p==rcen){
ve[center].erase(p);
break;
}
}
init(center);
}
else{
auto p=ve[rcen].begin();
if(center==rcen)center=lcen;
for(;p!=ve[rcen].end();p++){
if(*p==center){
ve[rcen].erase(p);
break;
}
}
init(rcen);
}
}
else{
int tag=ask(cen[0],cen[1]);
if(tag==0){
auto p=ve[cen[0]].begin();
for(;p!=ve[cen[0]].end();p++){
if(*p==cen[1]){
ve[cen[0]].erase(p);
break;
}
}
init(cen[0]);
}
else{
auto p=ve[cen[1]].begin();
for(;p!=ve[cen[1]].end();p++){
if(*p==cen[0]){
ve[cen[1]].erase(p);
break;
}
}
init(cen[1]);
}
}
}
cout<<"! "<<*trees.begin()<<endl;
cout.flush();
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
ll _ = 1;
cin >> _;
// cin.get();
while (_--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9724kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0 2 0 2 0 0 2
output:
? 3 5 ? 1 2 ! 1 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Memory Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 0 2 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 0 1 2 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 0 0 0 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 2 5 3 0 5 1 0 0 0 0 4 0 2 0 5 5 0 0 0 0 0 3 0 2 4 1 2 3 3 0 1 0 0 0
output:
? 2 8 ? 4 8 ? 3 4 ! 4 ? 3 7 ? 4 3 ? 1 2 ! 2 ? 1 8 ? 1 3 ? 1 5 ! 1 ? 2 5 ? 2 3 ! 3 ? 5 7 ? 1 4 ! 4 ? 5 1 ? 1 3 ! 1 ? 4 2 ? 1 5 ! 5 ? 1 5