QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#748627#9570. Binary TreeSkyEyeControllerWA 1ms5624kbC++233.1kb2024-11-14 20:54:252024-11-14 20:54:26

Judging History

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

  • [2024-11-14 20:54:26]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5624kb
  • [2024-11-14 20:54:25]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 mod = 998244353;
vector<vector<int>> con;
vector<int> ctr; // 重心
const int maxn = 2e5 + 9;
int n, m, allsz;
int sz[maxn], mss[maxn], fa[maxn];
void dfs(int u, int f, bool tag)
{
    fa[u] = f;
    sz[u] = 1, mss[u] = 0;
    if (tag)
    {
        auto it = find(con[u].begin(), con[u].end(), f);
        if (it != con[u].end())
            con[u].erase(it);
    }
    if (!con[u].empty())
        for (auto v : con[u])
        {
            dfs(v, u, tag);
            sz[u] += sz[v];
            mss[u] = max(mss[u], sz[v]);
        }
    mss[u] = max(mss[u], allsz - sz[u]);
    if (mss[u] <= allsz / 2)
    {
        ctr.push_back(u);
    }
} // 搜重心
void solve()
{
    cin >> n;
    con.assign(n + 1, vector<int>());
    ctr.clear();
    int rt = -1;
    for (int i = 1; i <= n; i++)
    {
        int l, r;
        cin >> l >> r;
        if (rt < 0)
            rt = i;
        if (l)
        {
            con[i].push_back(l);
            if (rt == l)
                rt = i;
        }
        if (r)
        {
            con[i].push_back(r);
            if (rt == r)
                rt = i;
        }
    }
    function<int(int, int)> ask = [&](int l, int r) -> int
    {
        cout << "? " << l << " " << r << '\n';
        cout.flush();
        int x;
        cin >> x;
        return x;
    };
    allsz = n;
    dfs(rt, 0, 1);
    int mid = ctr[0];
    while (sz[rt] > 1)
    {
        if (con[mid].size() == 2)
        {
            int l = con[mid][0], r = con[mid][1];
            int sig = ask(l, r);
            if (sig == 0)
            {
                ctr.clear();
                allsz = sz[l];
                dfs(rt = l, 0, 0);
                mid = ctr[0];
            }
            else if (sig == 2)
            {
                ctr.clear();
                allsz = sz[r];
                dfs(rt = r, 0, 0);
                mid = ctr[0];
            }
            else
            {
                allsz -= (sz[l] + sz[r]);
                ctr.clear();
                con[mid].clear();
                dfs(rt, 0, 0);
                mid = ctr[0];
            }
        }
        else
        {
            int l = mid, r = fa[mid];
            int sig = ask(l, r);
            if (sig == 0)
            {
                ctr.clear();
                allsz = sz[l];
                dfs(rt = l, 0, 0);
                mid = ctr[0];
            }
            else
            {
                auto it = find(con[r].begin(), con[r].end(), l);
                if (it != con[r].end())
                    con[r].erase(it);
                ctr.clear();
                allsz -= sz[l];
                dfs(rt, 0, 0);
                mid = ctr[0];
            }
        }
    }
    cout << "! " << rt << " " << '\n';
    cout.flush();
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Wrong Answer
time: 0ms
memory: 5624kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
1
2
0
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2

output:

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

result:

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