QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#736003#9570. Binary TreepaoxiaomoWA 3ms5900kbC++203.8kb2024-11-11 23:28:112024-11-11 23:28:11

Judging History

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

  • [2024-11-11 23:28:11]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:5900kb
  • [2024-11-11 23:28:11]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
// #define max(a, b) ((a) > (b) ? (a) : (b))
// #define min(a, b) ((a) < (b) ? (a) : (b))
#define pb push_back
#define LNF 1e18
#define INF 0x7fffffff
#define int long long
#define lowbit(x) ((x) & (-x))
#define abs(x) llabs(x)
// #define endl '\n'
#define Y() cout << "Yes" << endl
#define N() cout << "No" << endl
const db eps = 1e-9;
const int mod = 998244353;
const int MAXN = 2e5 + 5;
vector<pair<int, int>> pth[200005];
int siz[200005], maxp[200005], vis[200005], root, ans = 0, n, t;

void get_root(int pos, int fa, int total)
{
    siz[pos] = 1;
    maxp[pos] = 0;
    for (auto &[to, w] : pth[pos])
    {
        if (to == fa || vis[to])
            continue;
        get_root(to, pos, total);
        siz[pos] += siz[to];
        maxp[pos] = max(maxp[pos], siz[to]);
    }
    maxp[pos] = max(maxp[pos], total - siz[pos]);
    if (!root || maxp[pos] < maxp[root])
        root = pos;
}

int calc(int pos)
{
    int good = 0;
    vector<int> nu;
    for (auto &[to, w] : pth[pos])
    {
        if (!vis[to])
        {
            good++;
            nu.push_back(to);
        }
    }
    // cout << pos << " " << good << endl;
    if (good == 0)
    {
        cout << "! " << pos << endl;
        return -1;
    }
    else if (good == 1)
    {
        cout << "? " << nu[0] << " " << pos << endl;

        cin >> t;
        if (t == 0)
            cout << "! " << nu[0] << endl;
        else
            cout << "! " << pos << endl;
        return -1;
    }
    else if (good == 2)
    {
        cout << "? " << nu[0] << " " << nu[1] << endl;

        cin >> t;
        if (t == 1)
        {
            cout << "! " << pos << endl;
            return -1;
        }
        else if (t == 0)
        {
            return nu[0];
        }
        else
            return nu[1];
    }
    else
    {
        cout << "? " << nu[0] << " " << nu[1] << endl;

        cin >> t;
        if (t == 1)
        {
            return nu[2];
        }
        else if (t == 0)
        {
            return nu[0];
        }
        else
            return nu[1];
    }
}
void getsz(int pos, int fa)
{
    siz[pos] = 1;
    for (auto &[to, w] : pth[pos])
    {
        if (to == fa || vis[to])
            continue;
        getsz(to, pos);
        siz[pos] += siz[to];
    }
}
int cnt = 0;
void sol(int pos)
{
    // cnt++;
    // if(cnt>__lg(n))return;
    // cout << pos << endl;
    // for (int i = 1; i <= n; i++)
    // {
    //     cout << siz[i] << endl;
    // }
    int nu = calc(pos);
    if (nu == -1)
        return;
    // cout << siz[pos] << endl;
    getsz(pos, 0);
    if (t == 1)
    {

        // int masi = n;
        // cout << siz[pos] << endl;
        for(auto [to,w]:pth[pos]){
            if(to!=nu)vis[to]=1;
        }
        maxp[root = 0] = n + 1;
        get_root(pos, 0, siz[nu] + 1);
        sol(root);
    }
    else
    {

        vis[pos] = 1;
        maxp[root = 0] = n + 1;
        get_root(nu, 0, siz[nu]);
        sol(root);
    }
}

void add(int a, int b)
{
    pth[a].push_back({b, 1});
    pth[b].push_back({a, 1});
}
void solve()
{
    cin >> n;
    cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        if (a)
            add(a, i);
        if (b)
            add(b, i);
    }
    maxp[root = 0] = n + 1;
    get_root(1, 0, n);
    sol(root);
    for (int i = 1; i <= n; i++)
    {
        pth[i].clear();
        vis[i] = 0;
    }
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 5624kb

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
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

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

input:

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

output:

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

result:

wrong answer Too many queries (test case 90)