QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#750842#9570. Binary TreehuangxiWA 2ms3828kbC++202.9kb2024-11-15 16:06:402024-11-15 16:06:44

Judging History

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

  • [2024-11-15 16:06:44]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3828kb
  • [2024-11-15 16:06:40]
  • 提交

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 dis[N];
int pre[N];
int MAX;
void dfs(int u,int fa){
    for(auto e:vec[u]){
        if(e==fa||st[e]) continue;
        pre[e]=u;
        dis[e]=dis[u]+1;
        dfs(e,u);
    }
    if(dis[u]>dis[MAX]){
        MAX=u;
    }

}

int query(int s,int t){
    cout<<"? "<<s<<' '<<t<<endl<<flush;
    int x;
    cin>>x;
    return x;
}
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);
            }
        }
        int root=1;
        while(true) {
            MAX = 0;
            dis[root] = 0;
            dfs(root, -1);
            if (dis[MAX] == 0) {
                cout << "! " << root << endl << flush;
                break;
            }
            int s = MAX;
            MAX = 0;
            dis[s] = 0;
            dfs(s, -1);
            int t = MAX;
            int ans = query(s, t);
            if (ans == 1) {
                int d = dis[t] / 2;
                int xx;
                for (int i = t, j = 0; j <= d + 1; j++, i = pre[i]) {
                    if (j == d - 1) {
                        st[i] = true;
                    } else if (j == d + 1) {
                        st[i] = true;
                    } else if (j == d) {
                        xx = i;
                    }
                }
                root = xx;
            } else if (ans == 2) {
                int d = (dis[t] - 1) / 2;
                int xx;
                for (int i = t, j = 0; j <= d + 1; j++, i = pre[i]) {
                    if (j == d + 1) {
                        st[i] = true;
                    } else if (j == d) {
                        xx = i;
                    }
                }
                root = xx;
            } else {
                int d = dis[t] - (dis[t] - 1) / 2;

                int xx;
                for (int i = t, j = 0; j <= d; j++, i = pre[i]) {
                    if (j == d - 1) {
                        st[i] = true;
                    } else if (j == d) {
                        xx = i;
                    }
                }
                root = xx;
            }
        }
        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: 3796kb

input:

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

output:

? 4 1
? 1 5
! 2
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3828kb

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

output:

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

result:

wrong answer Too many queries (test case 20)