QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#761072#9570. Binary TreeMeowmeowmeow#RE 4ms16640kbC++203.6kb2024-11-18 20:47:592024-11-18 20:47:59

Judging History

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

  • [2024-11-18 20:47:59]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:16640kb
  • [2024-11-18 20:47:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define MAXN 200005
#define pb push_back

int n,m;
set<int>v[MAXN];
int s[MAXN];
int c[MAXN],fa[MAXN];

void dfs(int x,int ls) {
    s[x] = 1;
    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);
            }
        }
        int o = 1;
        int cnt = 0;
        while(1) {
            cnt ++;
            if(cnt > pp) {
                cout<<"wtf\n";
                return 0;
            }
            m = 0;
            fa[o] = 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;
                for(auto y:v[x]) {
                    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) return 0;
            if(v[t].size() == 1) {
                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;
            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: 100
Accepted
time: 4ms
memory: 16640kb

input:

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

output:

? 3 5
? 2 1
! 2
? 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
0
0
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
1
2
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
1
1
0
5
3 0
5 1
0 0
0 0
4 0
0
0
5
5 0
0 0
0 0
3 0
2 4
0
2
3
3 0
1 0
0 0
0
2
2 0
0 0
2
3
2 3
0 0
0 0
1
10
2 8
9 7
0 ...

output:

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

result: