QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#728651#9570. Binary Treeucup-team112#RE 0ms3604kbC++205.6kb2024-11-09 15:35:172024-11-09 15:35:19

Judging History

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

  • [2024-11-09 15:35:19]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3604kb
  • [2024-11-09 15:35:17]
  • 提交

answer

#include <bits/stdc++.h>
#include <bitset>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <random>
#include <set>
#include <type_traits>
#include <unordered_map>

using namespace std;
using ll = long long;

#define rep(i, n, m) for (ll(i) = (n); (i) < (m); (i)++)
#define rrep(i, n, m) for (ll(i) = (n); (i) > (m); (i)--)
const ll mod = 998244353;
const ll inf = 1e18;
const ll INF = 4e18 + 10;

using pll = pair<ll, ll>;

void pline(vector<int> lis) {
    rep(i, 0, lis.size()) {
        printf("%d", lis[i]);
        if (i != lis.size() - 1)
            printf(" ");
        else
            printf("\n");
    }
}

void pline(vector<ll> lis) {
    rep(i, 0, lis.size()) {
        printf("%lld", lis[i]);
        if (i != lis.size() - 1)
            printf(" ");
        else
            printf("\n");
    }
}

void pline2(vector<ll> lis) {
    rep(i, 0, lis.size()) {
        printf("%lld", lis[i]);
        if (i != lis.size() - 1)
            printf("");
        else
            printf("\n");
    }
}

void pline(vector<pair<ll, ll>> lis) {
    rep(i, 0, lis.size()) {
        printf("/%lld,%lld/", lis[i].first, lis[i].second);
        if (i != lis.size() - 1)
            printf(" ");
        else
            printf("\n");
    }
}

// 全方位1回目
void dfsA(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able, vector<ll> &csize) {
    csize[v] = 1;
    for (ll nex : lis[v]) {
        if (able[nex] && (nex != p)) {
            dfsA(nex, v, lis, able, csize);
            csize[v] += csize[nex];
        }
    }
}

// 全方位2回目
void dfsB(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able, vector<ll> &csize,
          vector<ll> &ablevs, vector<ll> &centorid) {

    ll cssum = 0;
    vector<ll> cslis;

    for (ll nex : lis[v]) {
        if (able[nex] && (nex != p)) {
            dfsB(nex, v, lis, able, csize, ablevs, centorid);
            cslis.push_back(csize[nex]);
            cssum += csize[nex];
        }
    }

    cslis.push_back(ablevs.size() - 1 - cssum);

    ll csmax = 0;
    for (ll tmp : cslis) {
        csmax = max(csmax, tmp);
    }

    if (csmax <= ablevs.size() / 2) {
        centorid.push_back(v);
    }
}

// 消去
void dfsC(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able) {
    for (ll nex : lis[v]) {
        if (able[nex] && (nex != p)) {
            dfsC(nex, v, lis, able);
        }
        able[v] = false;
    }
}

void dfsD(ll v, ll p, vector<vector<ll>> &lis, vector<bool> &able) {
    for (ll nex : lis[v]) {
        if (able[nex] && (nex != p)) {
            dfsC(nex, v, lis, able);
        }
        able[v] = false;
    }
}

int main() {

    ll TT;
    cin >> TT;

    rep(loop, 0, TT) {

        ll n;
        cin >> n;
        vector<vector<ll>> lis(n);

        rep(i, 0, n) {
            ll x, y;
            cin >> x >> y;
            x--;
            y--;

            if (x != -1) {
                lis[i].push_back(x);
                lis[x].push_back(i);
            }
            if (y != -1) {
                lis[i].push_back(y);
                lis[y].push_back(i);
            }
        }

        // 可能性がある頂点
        vector<bool> able(n, true);

        while (1) {

            // ありうる頂点
            vector<ll> ablevs(0);
            rep(v, 0, n) {
                if (able[v]) {
                    ablevs.push_back(v);
                }
            }

            if (ablevs.size() == 1) {
                cout << "! " << ablevs[0] + 1 << flush << endl;
                break;
            }

            // 重心を求める
            vector<ll> csize(n, 0);
            vector<ll> centorid(0);

            dfsA(ablevs[0], -1, lis, able, csize);
            dfsB(ablevs[0], -1, lis, able, csize, ablevs, centorid);

            if (centorid.size() == 2) {
                cout << "? " << centorid[0] + 1 << " " << centorid[1] + 1 << flush << endl;
                ll cat;
                cin >> cat;

                if (cat == 0) {
                    dfsC(centorid[1], centorid[0], lis, able);
                } else {
                    dfsC(centorid[0], centorid[1], lis, able);
                }
            } else if (centorid.size() == 1) {

                vector<pll> cvs;
                for (ll nex : lis[centorid[0]]) {
                    if (able[nex]) {
                        ll sss = min(csize[nex], (ll)ablevs.size() - csize[nex]);
                        cvs.push_back(make_pair(sss, nex));
                    }
                }

                sort(cvs.begin(), cvs.end());
                reverse(cvs.begin(), cvs.end());

                cout << "? " << cvs[0].second + 1 << " " << cvs[1].second + 1 << flush << endl;
                ll cat;
                cin >> cat;

                if (cat == 0) {
                    dfsC(cvs[1].second, centorid[0], lis, able);
                    if (cvs.size() >= 3) {
                        dfsC(cvs[2].second, centorid[0], lis, able);
                    }
                    able[centorid[0]] = false;
                } else if (cat == 2) {
                    dfsC(cvs[0].second, centorid[0], lis, able);
                    if (cvs.size() >= 3) {
                        dfsC(cvs[2].second, centorid[0], lis, able);
                    }
                    able[centorid[0]] = false;
                } else {
                    dfsC(cvs[1].second, centorid[0], lis, able);
                    dfsC(cvs[0].second, centorid[0], lis, able);
                }
            }
        }
    }
}

詳細信息

Test #1:

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

input:

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

output:

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

output:

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

result: