답안 #971404

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971404 2024-04-28T13:15:20 Z vjudge1 Prize (CEOI22_prize) C++17
0 / 100
2112 ms 386396 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")

using namespace std;

using vi = vector<int>;
using ii = pair<int, int>;

struct tree {
    int n; 
    vi par, niv, D;
    vector<vi> L, Anc;

    tree(int N) : n(N), L(N), par(N), niv(N) {}

    void add_edge(int u, int v) {
        L[u].push_back(v);
        L[v].push_back(u);
    }

    void set_dist(vi dist0) { D = dist0; }

    void init() {
        function<void(int, int, int)> dfs0 = [&](int u, int p, int li) {
            par[u] = p;
            niv[u] = li;
            for(auto it : L[u])
                if(it != p)  {
                    dfs0(it, u, li + 1);
                }
        };
        dfs0(0, -1, 0);
        Anc.push_back(par);
        for(int k = 1; (1 << k) <= n; ++k) {
            Anc.push_back(Anc.back());
            for(int i = 0; i < n; ++i)
                Anc[k][i] = Anc[k - 1][i] == -1 ? -1 : Anc[k - 1][Anc[k - 1][i]];
        }
    }

    int lift(int u, int nr) {
        for(int i = 0; i < Anc.size(); ++i)
            if(nr & (1 << i)) u = Anc[i][u];
        return u;
    }

    int lca(int u, int v) {
        if(niv[u] > niv[v]) swap(u, v);
        v = lift(v, niv[v] - niv[u]);
        if(u == v) return u;
        for(int i = int(Anc.size()) - 1; i >= 0; --i)
            if(Anc[i][u] != Anc[i][v]) {
                u = Anc[i][u];
                v = Anc[i][v];
            }
        return par[u];
    }

    int dist(int u, int v) {
        int l = lca(u, v);
        return D[u] + D[v] - 2 * D[l];
    }

    vi get_V(int k) {
        vi Re;
        function<void(int, int)> dfs = [&](int u, int p) {
            if(!k) return;
            Re.push_back(u);
            --k;
            for(auto it : L[u])
                if(it != p) {
                    dfs(it, u);
                }
        };
        dfs(0, -1);
        return Re;
    }
};
int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int n, k, q, t;
    cin >> n >> k >> q >> t;

    tree T1(n), T2(n);
    for(int i = 0; i < n; ++i) {
        int p;
        cin >> p;
        if(p != -1) T1.add_edge(i, p - 1);
    }
    for(int i = 0; i < n; ++i) {
        int p;
        cin >> p;
        if(p != -1) T2.add_edge(i, p - 1);
    }
    T1.init();
    T2.init();

    auto V = T1.get_V(k);
    for(auto it : V)
        cout << it + 1 << " ";
    cout << "\n";
    cout.flush();
    vi dist(n, 0);
    for(int i = 1; i < V.size(); ++i) {
        cout << "? " << V[0] + 1 << " " << V[i] + 1 << endl;
    }
    cout << "!" << endl;
    for(int i = 1; i < V.size(); ++i) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        dist[V[i]] = a + b;
    }
    cout.flush();
    T1.set_dist(dist);

    vi Re;
    for(int i = 0; i < q; ++i) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        int v = T1.dist(a, b);
        Re.push_back(v);
    }
  return 0;
    for(auto it : Re)
        cout << it << " " << it << "\n";
    cout.flush();
    return 0;
}

Compilation message

Main.cpp: In constructor 'tree::tree(int)':
Main.cpp:13:16: warning: 'tree::L' will be initialized after [-Wreorder]
   13 |     vector<vi> L, Anc;
      |                ^
Main.cpp:12:8: warning:   'vi tree::par' [-Wreorder]
   12 |     vi par, niv, D;
      |        ^~~
Main.cpp:15:5: warning:   when initialized here [-Wreorder]
   15 |     tree(int N) : n(N), L(N), par(N), niv(N) {}
      |     ^~~~
Main.cpp: In member function 'int tree::lift(int, int)':
Main.cpp:43:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |         for(int i = 0; i < Anc.size(); ++i)
      |                        ~~^~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:106:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |     for(int i = 1; i < V.size(); ++i) {
      |                    ~~^~~~~~~~~~
Main.cpp:110:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for(int i = 1; i < V.size(); ++i) {
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1046 ms 190320 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1054 ms 201296 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 876 ms 196156 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1894 ms 378856 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2112 ms 386396 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -