QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#760012#9570. Binary Treeyuanruiqi#TL 0ms6028kbC++142.6kb2024-11-18 14:03:162024-11-18 14:03:16

Judging History

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

  • [2024-11-18 14:03:16]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:6028kb
  • [2024-11-18 14:03:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int maxn = 100000 + 10;
vector<int> g[maxn];
bool vis[maxn];
// int siz[maxn], fa[maxn];
// int dfs(int u, int fa, int n)
// {
//     ::fa[u] = fa;
//     siz[u] = 1;
//     int ans = 0;
//     for (int v : g[u])
//     {
//         if (v == fa || vis[v]) continue;
//         int w = dfs(v, u, n);
//         siz[u] += siz[v];
//         v = w;
//         if (!ans || abs(siz[v] - (n - siz[v])) <= abs(siz[ans] - (n - siz[ans]))) ans = v;
//     }
//     if (!ans || abs(siz[u] - (n - siz[u])) <= abs(siz[ans] - (n - siz[ans]))) ans = u;
//     return ans;
// }
// int calc(int u)
// {
//     int v = fa[u];
//     cout << "? " << u << ' ' << v << endl;
//     int x;
//     cin >> x;
//     if (x == 2) swap(u, v);
//     vis[v] = 1;
//     dfs(u, v, siz[u]);
//     int w = dfs(u, v, siz[u]);
//     if (siz[u] == 1) return u;
//     return calc(w);
// }
int dx[maxn], dy[maxn];
void dfs(int u, int fa, int d[maxn])
{
    d[u] = d[fa] + 1;
    for (int v : g[u])
    {
        if (v == fa || vis[v]) continue;
        dfs(v, u, d);
    }
}
void solve()
{
    int n;
    cin >> n;
    for (int i=1;i<=n;++i) g[i].clear(), vis[i] = 0;
    for (int i=1;i<=n;++i)
    {
        int x, y;
        cin >> x >> y;
        if (x) g[i].emplace_back(x), g[x].emplace_back(i);
        if (y) g[i].emplace_back(y), g[y].emplace_back(i);
    }
    // int x = dfs(1, 0, n);
    // int p = calc(x);
    // cout << "! " << p << endl;
    for (;;)
    {
        int x = 0;
        for (int i=1;i<=n;++i)
            if (!vis[i])
            {
                x = i;
                break;
            }
        memset(dx, 0, sizeof(dx[0]) * (n + 5));
        memset(dy, 0, sizeof(dy[0]) * (n + 5));
        dfs(x, 0, dx);
        int y = x;
        for (int i=1;i<=n;++i)
            if (dx[i] > dx[y]) y = i;
        if (x == y)
        {
            cout << "! " << x << endl;
            return;
        }
        dfs(y, 0, dy);
        x = y;
        for (int i=1;i<=n;++i)
            if (dy[i] > dy[x]) x = i;
        memset(dx, 0, sizeof(dx[0]) * (n + 5));
        dfs(x, 0, dx);
        cout << "? " << x << ' ' << y << endl;
        int z;
        cin >> z;
        for (int i=1;i<=n;++i)
        {
            if (z == 0 && dx[i] >= dy[i]) vis[i] = 1;
            if (z == 1 && dx[i] != dy[i]) vis[i] = 1;
            if (z == 2 && dx[i] <= dy[i]) vis[i] = 1;
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 6028kb

input:

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

output:

? 1 4
? 1 5
! 2
? 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
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
1
0
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
0
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
1
5
3 0
5 1
0 0
0 0
4 0
0
2
5
5 0
0 0
0 0
3 0
2 4
0
1
3
3 0
1 0
0 0
2
2
2 0
0 0
2
3
2 3
0 0
0 0
1
10
2 8
9 7
0 0
...

output:

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

result: