QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#722178#9570. Binary Treeucup-team4474#TL 1ms3884kbC++202.9kb2024-11-07 18:04:582024-11-07 18:04:59

Judging History

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

  • [2024-11-07 18:04:59]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3884kb
  • [2024-11-07 18:04:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

bool Memory_begin;

bool Memory_end;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cerr << (&Memory_end - &Memory_begin) / 1048576.0 << "MB" << '\n';

    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector<vector<int>> adj(n + 1);
        for (int i = 1, x, y; i <= n; i++)
        {
            cin >> x >> y;
            if (x)
                adj[i].push_back(x), adj[x].push_back(i);
            if (y)
                adj[i].push_back(y), adj[y].push_back(i);
        }
        vector<int> vis(n + 1);

        int sum = n;
        while (sum > 1)
        {
            vector<int> dep(n + 1), siz(n + 1);
            auto dfs1 = [&](auto &dfs1, int cur, int fa, int sum) -> int
            {
                siz[cur] = 1;
                int mx = 0;
                for (int i : adj[cur])
                {
                    if (i == fa or vis[i])
                        continue;
                    int tmp = dfs1(dfs1, i, cur, sum);
                    if (tmp)
                        return tmp;
                    siz[cur] += siz[i];
                    mx = max(mx, siz[i]);
                }
                mx = max(mx, sum - siz[cur]);
                if (mx * 2 <= sum)
                    return cur;
                return 0;
            };
            auto dfs2 = [&](auto &dfs2, int cur, int fa) -> void
            {
                dep[cur] = dep[fa] + 1;
                for (int i : adj[cur])
                {
                    if (vis[i] or i == fa)
                        continue;
                    dfs2(dfs2, i, cur);
                }
            };
            for (int i = 1; i <= n; i++)
            {
                if (vis[i])
                    continue;
                int cur = dfs1(dfs1, i, 0, sum);
                int p1 = adj[cur][0], p2 = adj[cur].size() == 1 ? cur : adj[cur][1];
                dfs2(dfs2, p1, 0);
                auto dep1 = dep;
                dfs2(dfs2, p2, 0);
                auto dep2 = dep;
                cout << "? " << p1 << ' ' << p2 << endl;
                int tmp;
                cin >> tmp;
                for (int j = 1; j <= n; j++)
                {
                    if (vis[j])
                        continue;
                    if (tmp == 0 and dep1[j] >= dep2[j])
                        vis[j] = 1, sum--;
                    else if (tmp == 1 and dep1[j] != dep2[j])
                        vis[j] = 1, sum--;
                    else if (tmp == 2 and dep1[j] <= dep2[j])
                        vis[j] = 1, sum--;
                }
                break;
            }
        }

        for (int i = 1; i <= n; i++)
        {
            if (!vis[i])
            {
                cout << "! " << i << endl;
                break;
            }
        }
    }
}
/*
 */

詳細信息

Test #1:

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

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

output:

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

result: