QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#716309#9570. Binary TreeKJJD#RE 1ms3560kbC++202.6kb2024-11-06 14:55:292024-11-06 14:55:29

Judging History

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

  • [2024-11-06 14:55:29]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3560kb
  • [2024-11-06 14:55:29]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
vector<int> p[N];
int siz[N];
int root = 0;
int ma = 1e9;
int query(int u, int v)
{
    cout << "? " << u << ' ' << v << endl;
    int x;
    cin >> x;
    return x;
}
void answer(int x)
{
    cout << "! " << x << endl;
    return;
}
void dfs1(int u, int fa)
{
    siz[u] = 1;
    for (int v : p[u])
    {
        if (v == fa)
            continue;
        dfs1(v, u);
        siz[u] += siz[v];
    }
}
void dfs2(int u, int fa, int pre_s)
{
    int tmp = pre_s;
    int sum = pre_s;
    for (int v : p[u])
    {
        if (v == fa)
            continue;
        sum += siz[v];
        tmp = max(tmp, siz[v]);
    }
    if (tmp < ma)
    {
        ma = tmp;
        root = u;
    }
    for (int v : p[u])
    {
        if (v == fa)
            continue;
        dfs2(v, u, sum - siz[v]+1);
    }
}
void solve()
{
    int n, m, k;
    cin >> n;
    for (int i = 1; i <= n; i++)
        p[i].clear();
    vector<int> du(n + 1, 0);
    for (int i = 1; i <= n; i++)
    {
        int u, v;
        cin >> u >> v;
        du[u]++, du[v]++;
        if (u)
            p[i].push_back(u), p[u].push_back(i);
        if (v)
            p[i].push_back(v), p[v].push_back(i);
    }
    for (int i = 1; i <= n; i++)
        if (!du[i])
            root = i;
    ma = n;
    dfs1(root, 0);
    dfs2(root, 0, 0);
    int f1 = -1;
    while (p[root].size() >= 1)
    {
        auto tmp = p[root];
        if (tmp.size() < 2)
            tmp.push_back(root);
        f1 = query(tmp[0], tmp[1]);
        if (f1 == 0)
        {
            p[tmp[0]].erase(find(p[tmp[0]].begin(), p[tmp[0]].end(), root));
            root = tmp[0];
        }
        else if (f1 == 1)
        {
            p[root].clear();
            if (tmp.size() >= 3 && tmp[2] != root)
                p[root].push_back(tmp[2]);
        }
        else
        {
            if (tmp[1] != root)
            {
                p[tmp[1]].erase(find(p[tmp[1]].begin(), p[tmp[1]].end(), root));
                root = tmp[1];
            }
            else
            {
                break;
            }
        }
        ma = n;
        dfs1(root, 0);
        dfs2(root, 0, 0);
        // cerr << "root:" << ' ' << root << '\n';
    }
    answer(root);
}

int main()
{
    // #ifdef LOCAL
    //     freopen("data.in", "r", stdin);
    //     freopen("data.out", "w", stdout);
    // #endif
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

详细

Test #1:

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

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
? 2 1
! 1

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
2
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
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:

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

result: