Submission #727368

# Submission time Handle Problem Language Result Execution time Memory
727368 2023-04-20T13:40:51 Z bitaro1337 Prize (CEOI22_prize) C++14
0 / 100
2386 ms 405260 KB
//BITARO BITARO BITARO
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int K = 20, N = 1e6 + 5;
struct tree {
    int n, root, tt = 0;
    vector<ll> dist;
    vector<pair<int, ll>> adj[N];
    vector<int> g[N];
    int jump[N][K];
    vector<bool> vis;
    vector<int> time_in, depth;
    tree(int _n, int _root) {
        n = _n, root = _root;
    }
    void init() {
        dist.assign(n, 0);
        depth.assign(n, 0);
        vis.assign(n, false);
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < K; ++j) jump[i][j] = root;
        }
        time_in.assign(n, 0);
        dfs(root, -1);
        for(int j = 1; j < K; ++j) {
            for(int i = 0; i < n; ++i) {
                jump[i][j] = jump[jump[i][j - 1]][j - 1];
            }
        }
    }
    void add_edge(int a, int b) {
        if(a >= n || b >= n) exit(0);
        g[a].push_back(b);
    }
    void add_edge(int a, int b, ll w) {
        if(a >= n || b >= n) exit(0);
        adj[a].push_back({b, w});
    }
    void dfs(int u, int par) {
        time_in[u] = tt++;
        for(int v: g[u]) {
            if(v == par) continue;
            jump[v][0] = u;
            depth[v] = depth[u] + 1;
            dfs(v, u);
        }
    }
    void get_distances(int u) {
        vis[u] = true;
        for(auto x: adj[u]) {
            int v = x.first;
            ll w = x.second;
            if(vis[v]) continue;
            dist[v] = dist[u] + w;
            get_distances(v);
        }
    }
    int lca(int a, int b) {
        if(depth[a] < depth[b]) swap(a, b);
        for(int j = K - 1; j >= 0; --j) {
            if(depth[jump[a][j]] >= depth[b]) a = jump[a][j];
        }
        if(a == b) return a;
        for(int j = K - 1; j >= 0; --j) {
            if(jump[a][j] != jump[b][j]) {
                a = jump[a][j];
                b = jump[b][j];
            }
        }
        return jump[a][0];
    }
    ll get_depth(int u) {
        return dist[u];
    }
    int tm(int x) {return time_in[x];}

};
int time1[(int)1e6 + 5], time2[(int)1e6 + 5];
bool cmp1(int x, int y) {
    return time1[x] < time1[y];
}
bool cmp2(int x, int y) {
    return time2[x] < time2[y];
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k, q, t; cin >> n >> k >> q >> t;
    vector<int> p1(n), p2(n);
    int root1 = -1, root2 = -1;
    for(int i = 0; i < n; ++i) {
        cin >> p1[i]; --p1[i];
        if(p1[i] == -2) root1 = i;
    }
    for(int i = 0; i < n; ++i) {
        cin >> p2[i]; --p2[i];
        if(p2[i] == -2) root2 = i;
    }
    if(root1 == -1 || root2 == -1) exit(0);
    tree t1(n, root1), t2(n, root2);
    for(int i = 0; i < n; ++i) {
        if(p1[i] != -2) t1.add_edge(p1[i], i);
        if(p2[i] != -2) t2.add_edge(p2[i], i);
    }
    t1.init(); t2.init();
    vector<int> nodes(n); iota(nodes.begin(), nodes.end(), 0);
    for(int i = 0; i < n; ++i) {
        time1[i] = t1.tm(i);
        time2[i] = t2.tm(i);
    }
    sort(nodes.begin(), nodes.end(), cmp1);
    while((int)nodes.size() > k) nodes.pop_back();
    if(t1.tm(root2) >= k) {
        nodes.pop_back();
        nodes.push_back(root2);
    }

    for(int x: nodes) cout << x + 1 << " ";
    cout << "\n"; 
    cout.flush();
    
    sort(nodes.begin(), nodes.end(), cmp2);

    for(int i = 0; i < k - 1; ++i) {
        cout << "? " << nodes[i] + 1 << " " << nodes[i + 1] + 1 << "\n";
    }
    cout << "!\n"; 
    cout.flush();
    for(int i = 0; i < k - 1; ++i) {
        {
            int u = nodes[i], v = nodes[i + 1];
            int l = t1.lca(u, v);
            int c1, c2; cin >> c1 >> c2;
            if(l != u) t1.add_edge(l, u, c1);
            if(l != v) t1.add_edge(l, v, c2);
            if(l != u) t1.add_edge(u, l, -c1);
            if(l != v) t1.add_edge(v, l, -c2);
        }
        {
            int u = nodes[i], v = nodes[i + 1];
            int l = t2.lca(u, v);
            int c1, c2; cin >> c1 >> c2;
            if(l != u) t2.add_edge(l, u, c1);
            if(l != v) t2.add_edge(l, v, c2);
            if(l != u) t2.add_edge(u, l, -c1);
            if(l != v) t2.add_edge(v, l, -c2);
        }
    }
    t1.get_distances(root1);
    t2.get_distances(root2);
    vector<pair<int, int>> queries(t);
    for(int i = 0; i < t; ++i) {
        cin >> queries[i].first >> queries[i].second; 
        --queries[i].first;
        --queries[i].second;
    }
    for(int i = 0; i < t; ++i) {
        cout << 0 << " " << 0 << "\n";
    }
    cout.flush();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1065 ms 330316 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1312 ms 332472 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 922 ms 323752 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1950 ms 396764 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2386 ms 405260 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -