QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#786608#9570. Binary TreeAndyqian7WA 1ms3668kbC++203.6kb2024-11-26 22:20:022024-11-26 22:20:04

Judging History

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

  • [2024-11-26 22:20:04]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3668kb
  • [2024-11-26 22:20:02]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i, s, e) for (int i = s; i <= e; i++)
using namespace std;
const int N = 1e5 + 10;
int n, sz[N], rt, mi, vis[N], tot;
vector<int> g2[N], g3[N];
void dfs(int s, int fa)
{
    sz[s] = 1;
    int cur = 0;
    for (int son : g2[s])
    {
        if (son == fa)
            continue;
        dfs(son, s);
        sz[s] += sz[son];
        cur = max(cur, sz[son]);
    }
    cur = max(cur, tot - sz[s]);
    if (cur < mi)
        rt = s, mi = cur;
}
void dfs2(int s, int fa)
{
    g3[s].clear();
    for (int son : g2[s])
    {
        if (son == fa || vis[son])
            continue;
        g3[s].push_back(son);
        dfs2(son, s);
    }
}
int query(int u, int v)
{
    cout << "? " << u << " " << v << endl;
    int t;
    cin >> t;
    return t;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        tot = n;
        rep(i, 1, n)
        {
            vis[i] = 0;
            g2[i].clear();
        }
        rep(i, 1, n)
        {
            int x, y;
            cin >> x >> y;
            if (x)
                g2[i].push_back(x), g2[x].push_back(i);
            if (y)
                g2[i].push_back(y), g2[y].push_back(i);
        }
        mi = 1e9;
        dfs(1, 0), dfs2(rt, 0);
        while (tot)
        {
            mi = 1e9;
            vis[rt] = 1;
            switch (g3[rt].size())
            {
            case 3:
                rep(i, 0, 2)
                {
                    rep(j, 0, 1)
                    {
                        if (sz[g3[rt][j]] < sz[g3[rt][j + 1]])
                            swap(g3[rt][j], g3[rt][j + 1]);
                    }
                }
                switch (query(g3[rt][0], g3[rt][1]))
                {
                case 0:
                    tot = sz[g3[rt][0]];
                    dfs(g3[rt][0], 0), dfs2(rt, 0);
                    break;
                case 1:
                    g2[rt] = {g3[rt][2]};
                    vis[rt] = 0;
                    dfs(rt, 0), dfs2(rt, 0);
                    break;
                case 2:
                    tot = sz[g3[rt][1]];
                    dfs(g3[rt][1], 0), dfs2(rt, 0);
                    break;
                default:
                    break;
                }
                break;
            case 2:
                switch (query(g3[rt][0], g3[rt][1]))
                {
                case 0:
                    tot = sz[g3[rt][0]];
                    dfs(g3[rt][0], 0), dfs2(rt, 0);
                    break;
                case 1:
                    cout << "! " << rt << endl;
                    tot = 0;
                    break;
                case 2:
                    tot = sz[g3[rt][1]];
                    dfs(g3[rt][1], 0), dfs2(rt, 0);
                    break;
                default:
                    break;
                }
                break;
            case 1:
                switch (query(rt, g3[rt][0]))
                {
                case 0:
                    cout << "! " << rt << endl;
                    tot = 0;
                    break;
                case 2:
                    cout << "! " << g3[rt][0] << endl;
                    tot = 0;
                    break;
                default:
                    break;
                }
                break;
            case 0:
                cout << "! " << rt << endl;
                tot = 0;
                break;
            default:
                break;
            }
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3668kb

input:

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

output:

? 1 3
! 1

result:

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