답안 #708671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
708671 2023-03-12T07:01:45 Z becaido Prize (CEOI22_prize) C++17
0 / 100
2283 ms 318264 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int SIZE = 1e6 + 5;
const int H = __lg(SIZE) + 1;

int n, k, q, t;
int dep[SIZE], ans[SIZE];

struct Tree {
    vector<int> adj[SIZE];
    int root, to[SIZE][H + 1], h[SIZE];
    void init() {
        FOR (i, 1, n) {
            int pa;
            cin >> pa;
            if (pa != -1) adj[pa].pb(i);
            else root = i;
        }
    }
    void dfs(int pos) {
        for (int np : adj[pos]) {
            h[np] = h[pos] + 1;
            to[np][0] = pos;
            dfs(np);
        }
    }
    void build() {
        h[root] = 1;
        dfs(root);
        FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];
    }
    int jump(int pos, int k) {
        int cnt = 0;
        while (k) {
            if (k & 1) pos = to[pos][cnt];
            cnt++;
            k >>= 1;
        }
        return pos;
    }
    int lca(int a, int b) {
        if (h[a] < h[b]) swap(a, b);
        a = jump(a, h[a] - h[b]);
        if (a == b) return a;
        for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
            a = to[a][i];
            b = to[b][i];
        }
        return to[a][0];
    }
} tree1, tree2;

void solve() {
    cin >> n >> k >> q >> t;
    tree1.init();
    tree2.init();
    tree1.build();
    tree2.build();

    vector<int> V;
    auto dfs = [&](auto dfs, int pos)->void {
        if (V.size() == k) return;
        V.pb(pos);
        for (int np : tree1.adj[pos]) dfs(dfs, np);
    };
    dfs(dfs, tree1.root);
    for (int x : V) {
        cout << x;
        if (x == V.back()) cout << '\n';
        else cout << ' ';
    }
    FOR (i, 1, k - 1) cout << "? " << tree1.root << ' ' << V[i] << '\n';
    cout << "!" << '\n';
    FOR (i, 1, k - 1) {
        int foo;
        cin >> foo >> dep[V[i]] >> foo >> foo;
    }

    FOR (i, 1, t) {
        int a, b;
        cin >> a >> b;
        int c = tree1.lca(a, b);
        ans[i] = dep[a] - dep[c] + dep[b] - dep[c];
    }
    FOR (i, 1, t) cout << ans[i] << '\n';
}

int main() {
    solve();
}

Compilation message

Main.cpp: In instantiation of 'solve()::<lambda(auto:23, int)> [with auto:23 = solve()::<lambda(auto:23, int)>]':
Main.cpp:85:24:   required from here
Main.cpp:81:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   81 |         if (V.size() == k) return;
      |             ~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1162 ms 183164 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1355 ms 183240 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1116 ms 181112 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2143 ms 316360 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2283 ms 318264 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -