Submission #754723

# Submission time Handle Problem Language Result Execution time Memory
754723 2023-06-08T12:47:33 Z Desh03 Factories (JOI14_factories) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;

vector<long long> d;
vector<int> dep;
vector<vector<pair<int, int>>> g;
vector<vector<int>> up;
int lg;

void dfs(int u) {
    for (int i = 1; i < lg; i++) up[i][u] = up[i - 1][up[i - 1][u]];
    for (auto [v, w] : g[u])
        if (v ^ up[0][u])
            d[v] = d[u] + w, dep[v] = dep[u] + 1, up[0][v] = u, dfs(v);
}

int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int k = dep[u] - dep[v];
    for (int i = 0; i < lg; i++)
        if (k >> i & 1)
            u = up[i][u];
    if (u == v) return u;
    for (int i = lg - 1; i >= 0; i--)
        if (up[i][u] ^ up[i][v])
            u = up[i][u], v = up[i][v];
    return up[0][u];
}

long long dist(int u, int v) {
    return d[u] + d[v] - (d[lca(u, v)] << 1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    while ((1 << lg) <= n) ++lg;
    g.resize(n), d.resize(n), dep.resize(n);
    up = vector<vector<int>> (lg, vector<int> (n));
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(0);
    while (q--) {
        int a, b;
        cin >> a >> b;
        vector<int> u(a), v(b);
        for (int &x : u) cin >> x;
        for (int &x : v) cin >> x;
        long long ans = 1e18;
        for (int i : u)
            for (int j : v)
                ans = min(ans, dist(i, j));
        cout << ans << '\n';
    }
}

Compilation message

/usr/bin/ld: /tmp/cc6bJEE5.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccEpVV14.o:factories.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cc6bJEE5.o: in function `main':
grader.cpp:(.text.startup+0x37d): undefined reference to `Init(int, int*, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x412): undefined reference to `Query(int, int*, int, int*)'
collect2: error: ld returned 1 exit status