QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#786678#9570. Binary TreeAndyqian7WA 3ms5676kbC++203.5kb2024-11-26 22:50:102024-11-26 22:50:11

Judging History

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

  • [2024-11-26 22:50:11]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5676kb
  • [2024-11-26 22:50:10]
  • 提交

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;
void dfs(int s, int fa)
{
    sz[s] = 1;
    int cur = 0;
    for (int son : g2[s])
    {
        if (son == fa || vis[son])
            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 cal(int s)
{
    g3.clear();
    for (int son : g2[s])
    {
        if (vis[son])
            continue;
        g3.push_back(son);
    }
}
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);
        while (tot)
        {
            mi = 1e9;
            vis[rt] = 1;
            cal(rt);
            switch (g3.size())
            {
            case 3:
                rep(i, 0, 2)
                {
                    rep(j, 0, 1)
                    {
                        if (sz[g3[j]] < sz[g3[j + 1]])
                            swap(g3[j], g3[j + 1]);
                    }
                }
                switch (query(g3[0], g3[1]))
                {
                case 0:
                    tot = sz[g3[0]];
                    dfs(g3[0], 0);
                    break;
                case 1:
                    vis[g3[0]] = vis[g3[1]] = 1;
                    vis[rt] = 0;
                    tot = sz[g3[2]] + 1;
                    dfs(rt, 0);
                    break;
                case 2:
                    tot = sz[g3[1]];
                    dfs(g3[1], 0);
                    break;
                default:
                    break;
                }
                break;
            case 2:
                switch (query(g3[0], g3[1]))
                {
                case 0:
                    tot = sz[g3[0]];
                    dfs(g3[0], 0);
                    break;
                case 1:
                    cout << "! " << rt << endl;
                    tot = 0;
                    break;
                case 2:
                    tot = sz[g3[1]];
                    dfs(g3[1], 0);
                    break;
                default:
                    break;
                }
                break;
            case 1:
                switch (query(rt, g3[0]))
                {
                case 0:
                    cout << "! " << rt << endl;
                    tot = 0;
                    break;
                case 2:
                    cout << "! " << g3[0] << endl;
                    tot = 0;
                    break;
                default:
                    break;
                }
                break;
            case 0:
                cout << "! " << rt << endl;
                tot = 0;
                break;
            default:
                break;
            }
        }
    }
}

詳細信息

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 3648kb

input:

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

output:

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

result:

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