QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#748597#9570. Binary TreeSkyEyeControllerTL 1ms3524kbC++233.0kb2024-11-14 20:49:262024-11-14 20:49:27

Judging History

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

  • [2024-11-14 20:49:27]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3524kb
  • [2024-11-14 20:49:26]
  • 提交

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();
    for (int i = 1; i <= n; i++)
    {
        int l, r;
        cin >> l >> r;
        if (l)
        {
            con[i].push_back(l);
            con[l].push_back(i);
        }
        if (r)
        {
            con[i].push_back(r);
            con[r].push_back(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(1, 0, 1);
    int mid = ctr[0];
    int rt = 1;
    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: 3524kb

input:

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

output:

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

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

output:

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

result: