QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#769241#9570. Binary TreezsddsTL 0ms9980kbC++142.0kb2024-11-21 16:42:562024-11-21 16:42:56

Judging History

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

  • [2024-11-21 16:42:56]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:9980kb
  • [2024-11-21 16:42:56]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define db double
#define fi first
#define se second
#define pb push_back
using namespace std;

ll read() {
    ll x = 0, f = 1; char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
    return x * f;
}

ll gcd(ll x, ll y) {return y ? gcd(y, x % y) : x;}

ll rt, ct, fg[200010], zx,  fa[200010];
ll n, sn[2][200010], siz[200010], num;

void dfs(int u) {
    num++; if(num > 1e7) return ;
    siz[u] = 1; int flag = 0;
    for(int i = 0; i < 2; i++) {
        if(!sn[i][u] || fg[sn[i][u]]) continue;
        dfs(sn[i][u]); siz[u] += siz[sn[i][u]];
        if(siz[sn[i][u]] > ct / 2) flag = 1;
    }
    if(ct - siz[u] > ct / 2) flag = 1;
    if(!flag) zx = u;
}

void solve() {
    n = read(); ct = n; rt = 1;
    for(int i = 1; i <= n; i++) fa[i] = siz[i] = fg[i] = 0;
    for(int i = 1; i <= n; i++) {
        sn[0][i] = read(), sn[1][i] = read();
        fa[sn[0][i]] = fa[sn[1][i]] = i;
    }
    for(int i = 1; i <= n; i++) if(!fa[i]) rt = i;
    while(ct > 1) {
        dfs(rt); int u = fa[zx], v = fa[zx], ck;
        if(!fa[zx] || fg[fa[zx]]) u = v = zx;
        if(sn[0][zx] && !fg[sn[0][zx]]) u = sn[0][zx]; 
        if(sn[1][zx] && !fg[sn[1][zx]]) v = sn[1][zx];
        printf("? %d %d\n", u, v); fflush(stdout); ck = read();
        if(ck == 1) {
            if(u == fa[zx] || v == fa[zx]) {printf("! %lld\n", zx); break;}
            fg[u] = fg[v] = 1; ct -= siz[u] + siz[v]; continue;
        }
        if(ck == 2) swap(u, v);
        if(ct == 2) {printf("! %d\n", u); break;}
        if(u == fa[zx]) {fg[zx] = 1; siz[u] -= siz[zx]; continue;}
        rt = u; ct = siz[u]; fg[fa[u]] = 1;
    }
    if(ct == 1) printf("! %lld\n", rt);
    fflush(stdout);
}

int main() {
    for(int T = read(); T; T--) solve();
    return 0;
}
/*
2
5
0 0
1 5
2 4
0 0
0 0
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9980kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

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

output:

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

result: