QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#751210#9570. Binary TreehuangxiRE 1ms3764kbC++202.6kb2024-11-15 17:30:392024-11-15 17:30:39

Judging History

你现在查看的是最新测评结果

  • [2024-11-15 17:30:39]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3764kb
  • [2024-11-15 17:30:39]
  • 提交

answer

#include<bits/stdc++.h>
#define ls p*2
#define rs p*2+1
#define x first
#define y second
#define endl '\n'
using namespace std;

mt19937 rnd(time(0));
typedef long long LL;
typedef unsigned long long  uLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
const double PI=acos(-1);
const int N=2e5+10,M=4e5,MOD=1e9+7,NN=1e6+10;
int T;
int n;
bool st[N];
vector<int>vec[N];
int dp[N];
bool flag;
int root;
int sum;
void modify(int u,int v,int mid,int op){
    if(op==1){
        root=mid;
        st[u]=true;
        st[v]=true;
    }else if(op==0){
        root=u;
        st[v]=true;
    }else{
        root=v;
        st[u]=true;
    }
}
int query(int s,int t){
    cout<<"? "<<s<<' '<<t<<endl<<flush;
    int x;
    cin>>x;
    return x;
}
bool cmp(PII a,PII b){
    return a.x>b.x;
}
void dfs(int u,int fa){
    if(flag) return;
    dp[u]=1;
    for(auto e:vec[u]){
        if(flag) return;
        if(e==fa||st[e]) continue;
        dfs(e,u);
        dp[u]+=dp[e];
    }
    if(flag) return;
    if(dp[u]*2==sum){
        int xx=query(u,fa);
        modify(u,fa,u,xx);
        flag=true;
        return;
    }else if(dp[u]*2>sum){
        PII cur[3]={{0,0},{0,0},{0,0}};
        for(int i=0;i<vec[u].size();i++){
            if(vec[u][i]!=fa){
                cur[i]={dp[vec[u][i]],vec[u][i]};
            }else{
                cur[i]={sum-dp[u],vec[u][i]};
            }
        }
        sort(cur,cur+3,cmp);
        int son=cur[0].y;
        int other=cur[1].y;
        int xx= query(son,other);
        modify(son,other,u,xx);
        flag=true;
        return;
    }
}
void calc(int u,int pa){
    sum++;
    dp[u]=0;
    for(auto e:vec[u]){
        if(e==pa||st[e]) continue;
        calc(e,u);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>T;
    while(T--){
        cin>>n;
        for(int i=1;i<=n;i++){
            int u,v;
            cin>>u>>v;
            if(u!=0){
                vec[u].push_back(i);
                vec[i].push_back(u);
            }
            if(v!=0){
                vec[v].push_back(i);
                vec[i].push_back(v);
            }
        }
        root=1;
        while(true) {
            flag=false;
            sum=0;
            calc(root,0);

            if(sum==1){
                cout<<"! "<<root<<endl<<flush;
                break;
            }
            dfs(root,0);

        }
        for(int i=1;i<=n;i++){
            vec[i].clear();
            st[i]=false;
        }
    }


}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3764kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
1
0
2
0 2
0 0
2

output:

? 3 1
? 5 2
! 5
? 2 1
! 1

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
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
2
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
1
0
5
4 5
3 1
0 0
0 0
0 0
0
2

output:

? 8 2
? 6 2
? 7 6
! 6
? 7 3
? 8 5
? 7 5
! 7
? 8 1
? 4 2
? 8 6
! 8
? 2 4
? 1 2
? 3 2

result: