QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#732807#9565. Birthday GiftLzy_#RE 0ms0kbC++143.5kb2024-11-10 15:57:292024-11-10 15:57:32

Judging History

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

  • [2024-11-10 15:57:32]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-10 15:57:29]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10;
int n , cnt , T;
int lft , rt , minn;
int head[N] , pre[N] , siz[N];
bool del[N];

struct Edge {
    int to , nxt;
}e[N << 1];

void add(int u , int v) {
    e[++cnt].to = v;
    e[cnt].nxt = head[u];
    head[u] = cnt;
}

void getrt(int now , int fa) {
    siz[now] = 1;
    int maxsiz = 0;
    for (int i = head[now]; i; i = e[i].nxt) {
        int y = e[i].to;
        if (y == fa || del[y]) continue;
        getrt(y , now);
        maxsiz = max(maxsiz , siz[y]);
        siz[now] += siz[y];
    }
    maxsiz = max(maxsiz , lft - siz[now]);
    if (maxsiz < minn) {
        rt = now;
        minn = maxsiz;
    }
    return;
}

void dfs(int now , int fa) {
    siz[now] = 1;
    for (int i = head[now]; i; i = e[i].nxt) {
        int y = e[i].to;
        if (y == fa || del[y]) continue;
        dfs(y , now);
        siz[now] += siz[y];
    }
}

void dele(int now , int fa) {
    if (now == -1 or del[now]) return;
    del[now] = 1; --lft;
    for (int i = head[now]; i; i = e[i].nxt) {
        int y = e[i].to;
        if (y == fa) continue;
        dele(y , now);
    }
}

void solve() {
    scanf("%d" , &n);
    cnt = 0; lft = n;
    for (int i = 1; i <= n; ++i) head[i] = del[i] = 0;
    for (int i = 1; i <= n; ++i) {
        int lson , rson;
        scanf("%d%d" , &lson , &rson);
        if (lson) {
            add(i , lson); add(lson , i);
        }
        if (rson) {
            add(i , rson); add(rson , i);
        }
    }
    int lgp = 0;
    rt = 1; minn = INT_MAX;
    while (n >> lgp) {
        lgp++;
        minn = INT_MAX;
        for (int i = 1; i <= n; ++i) siz[i] = 0;
        getrt(rt , 0);
        for (int i = 1; i <= n; ++i) siz[i] = 0;
        dfs(rt , 0);
        int fir = -1 , sizfir = 0;
        int sec = -1 , sizsec = 0;
        int thir = -1;
        for (int i = head[rt]; i; i = e[i].nxt) {
            int y = e[i].to;
            if (del[y]) continue;
            if (siz[y] >= sizfir) {
                thir = sec;
                sec = fir; sizsec = sizfir;
                fir = y; sizfir = siz[y];
            }
            else if (siz[y] >= sizsec) {
                thir = sec;
                sec = y; sizsec = siz[y];
            }
            else thir = y;
        }
        if (fir == -1) {
            printf("! %d\n" , rt);
            fflush(stdout);
            return;
        }
        else {
            if (sec == -1) {
                printf("? %d %d\n" , rt , fir);
                fflush(stdout);
                int res; scanf("%d" , &res);
                if (res == 0) printf("! %d\n" , rt);
                else printf("! %d\n" , fir);
                fflush(stdout);
                return;
            }
            else {
                printf("? %d %d\n" , fir , sec);
                fflush(stdout);
                int res; scanf("%d" , &res);
                if (res == 0) {
                    dele(sec , rt);
                    dele(thir , rt);
                    del[rt] = 1; --lft;
                    rt = fir;
                }
                else if (res == 2) {
                    dele(fir , rt);
                    dele(thir , rt);
                    del[rt] = 1; --lft;
                    rt = sec;
                }
                else {
                    dele(fir , rt);
                    dele(sec , rt);
                }
            }
        }
    }
}

signed main() {
    scanf("%d" , &T);
    while (T--) solve();
}

詳細信息

Test #1:

score: 0
Runtime Error

input:

5
0110101
01020102
0000021111
1012121010
0100202010

output:


result: