QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#739289#9570. Binary Treewifi32767RE 1ms3544kbC++203.2kb2024-11-12 21:21:102024-11-12 21:21:10

Judging History

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

  • [2024-11-12 21:21:10]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3544kb
  • [2024-11-12 21:21:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
// #define endl '\n'
using ll = long long;
const int MAX = 3e5 + 5;

void ASSERT(bool x){
    if (!x) while (1) cout << "ASSERTION FAILED" << endl;
}
void solve(){
    int n; cin >> n;
    vector<set<int>> graph(n + 1);
    for (int i = 1; i <= n; i ++){
        int x, y; cin >> x >> y;
        if (x) graph[x].insert(i), graph[i].insert(x);
        if (y) graph[y].insert(i), graph[i].insert(y);
    }
    int t = n, cur = 1;
    vector<int> siz(n + 1);
    function<void(int, int)> dfs1 = [&](int u, int p){
        siz[u] = 1;
        for (int v : graph[u]){
            if (v == p) continue;
            dfs1(v, u);
            siz[u] += siz[v];
        }
    };
    int p = 0;
    function<void(int, int)> dfs2 = [&](int u, int pre){
        int maxx = t - siz[u];
        for (int v : graph[u]){
            if (v == pre) continue;
            maxx = max(maxx, siz[v]);
            dfs2(v, u);
        }
        // cerr << u << ' ' << maxx << ' ' << t << ' ' << endl;
        if (maxx <= t / 2) p = u;
    };
    dfs1(1, 0);
    while (t > 1){
        p = cur;
        dfs2(cur, 0);
        // cout << p << ' ' << t << endl;
        if (graph[p].size() == 3){
            array<int, 3> a;
            int it = 0;
            for (int j : graph[p]){
                a[it] = j;
                it ++;
            }
            cout << "? " << a[0] << ' ' << a[2] << endl;
            int x; cin >> x;
            ASSERT(graph[p].count(a[0]));
            ASSERT(graph[p].count(a[2]));
            ASSERT(graph[a[0]].count(p));
            ASSERT(graph[a[2]].count(p));
            graph[p].erase(a[0]);
            graph[p].erase(a[2]);
            graph[a[0]].erase(p);
            graph[a[2]].erase(p);
            cur = a[x];
        }
        else if (graph[p].size() == 2){
            array<int, 2> a;
            int it = 0;
            for (int j : graph[p]){
                a[it] = j;
                it ++;
            }
            cout << "? " << a[0] << ' ' << a[1] << endl;
            int x; cin >> x;
            ASSERT(graph[p].count(a[0]));
            ASSERT(graph[p].count(a[1]));
            ASSERT(graph[a[0]].count(p));
            ASSERT(graph[a[1]].count(p));
            graph[p].erase(a[0]);
            graph[p].erase(a[1]);
            graph[a[0]].erase(p);
            graph[a[1]].erase(p);
            if (x == 0) cur = a[0];
            else if (x == 2) cur = a[1];
            else{
                cout << "! " << p << endl;
                return;
            }
        }
        else if (graph[p].size() == 1){
            int i;
            for (int j : graph[p]) i = j;
            cout << "? " << i << ' ' << p << endl;
            int x; cin >> x;
            if (x == 0) cout << "! " << i << endl;
            else if (x == 2) cout << "! " << p << endl;
            return;
        }
        else{
            cout << "! " << p << endl;
            return;
        }
        dfs1(cur, 0);
        t = siz[cur];
    }
    cout << "! " << cur << endl;
}
signed main(){
    // ios::sync_with_stdio(0);cin.tie(0);
    // cout << fixed << setprecision(10);
    int T;cin>>T;while (T--)
        solve();
}

詳細信息

Test #1:

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

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

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
2
2
2
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
2
1
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:

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

result: