QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#883824#9570. Binary TreexuemanWA 3ms5580kbC++233.3kb2025-02-05 19:12:152025-02-05 19:12:16

Judging History

This is the latest submission verdict.

  • [2025-02-05 19:12:16]
  • Judged
  • Verdict: WA
  • Time: 3ms
  • Memory: 5580kb
  • [2025-02-05 19:12:15]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
// #define endl '\n'
#define ll long long
const int N = 5e5 + 10;
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;

struct edge
{
    int to, ne;
} e[N];
int head[maxn], ecnt = 1;

void add(int x, int y)
{
    e[++ecnt].to = y;
    e[ecnt].ne = head[x];
    head[x] = ecnt;
}

int n, sum;
int sz[maxn];
int ans, pos;

void dfs1(int x, int fa, int d1, int d2)
{
    sz[x] = 1;
    int res = 0;
    for (int i = head[x]; i; i = e[i].ne)
    {
        int y = e[i].to;
        if (y != fa && y != d1 && y != d2)
        {
            dfs1(y, x, d1, d2);
            sz[x] += sz[y];
            res = max(res, sz[y]);
        }
    }
    res = max(res, n - sz[x]);
    if (res < ans || (ans == res && x < pos))
        ans = res, pos = x;
}

void dfs2(int x, int fa, int d1, int d2)
{
    sz[x] = 1;
    for (int i = head[x]; i; i = e[i].ne)
    {
        int y = e[i].to;
        if (y == fa || y == d1 || y == d2)
            continue;
        dfs2(y, x, d1, d2);
        sz[x] += sz[y];
    }
}

int ask(int x, int y)
{
    cout << "? " << x << ' ' << y << endl;
    int tmp;
    cin >> tmp;
    return tmp;
}

void solve(int now, int d1, int d2 = 0)
{
    ans = inf, pos = 0;
    dfs1(now, 0, d1, d2);
    // cout << '(' << pos << ')' << ans << endl;
    // for (int i = 1; i <= n; i++)
    //     cout << sz[i] << ' ';
    // cout << endl;
    dfs2(pos, 0, d1, d2);

    vector<pair<int, int>> v;
    for (int i = head[pos]; i; i = e[i].ne)
    {
        int y = e[i].to;
        if (y == d1 || y == d2)
            continue;
        v.push_back({sz[y], y});
    }
    sort(v.begin(), v.end(), greater<pair<int, int>>());
    if (v.size() == 3)
    {
        int op = ask(v[0].second, v[1].second);
        if (op == 0)
            solve(v[0].second, pos);
        else if (op == 1)
            solve(v[2].second, v[0].second, v[1].second);
        else
            solve(v[1].second, pos);
    }
    else if (v.size() == 2)
    {
        int op = ask(v[0].second, v[1].second);
        if (op == 0)
            solve(v[0].second, pos);
        else if (op == 1)
        {
            sum = pos;
            return;
        }
        else
            solve(v[1].second, pos);
    }
    else if (v.size() == 1)
    {
        int op = ask(pos, v[0].second);
        if (op == 0)
        {
            sum = pos;
            return;
        }
        else
        {
            sum = v[0].second;
            return;
        }
    }
    else
    {
        sum = pos;
        return;
    }
}

int main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);
    // cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        for (int i = 0; i <= n; i++)
            head[i] = 0;
        ecnt = 1;
        for (int i = 1; i <= n; i++)
        {
            int x, y;
            cin >> x >> y;
            if (x)
                add(x, i), add(i, x);
            if (y)
                add(y, i), add(i, y);
        }
        if (n == 1)
        {
            cout << "! 1" << endl;
            continue;
        }
        sum = 0;
        solve(1, 0);
        cout << "! " << sum << endl;
    }
}

詳細信息

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 5580kb

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
2

output:

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

result:

wrong answer There are 3 candidates. (test case 2)