Cod sursa(job #3340931)

Utilizator jumaracosminJumara Cosmin-Mihai jumaracosmin Data 17 februarie 2026 12:04:18
Problema Radiatie Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.44 kb
#include <bits/stdc++.h>

std::ifstream fin("radiatie.in");
std::ofstream fout("radiatie.out");

const int MOD = 1e9 + 7;
const int INF = 1e9 + 5;
const int64_t LONG_INF = static_cast<int64_t>(1e18) + 5;

const int NMAX = 15e3 + 5;
const int LOG = 15;
const int LOGMAX = 17;

struct edge {
    int src, dest, cost;
    bool operator<(const edge &other) const {
        return cost < other.cost;
    }
};

struct DSU {
    std::vector<int> father, sz;

    void init(int _sz) {
        sz.assign(_sz + 5, 1);
        father.resize(_sz + 5);
        std::iota(father.begin(), father.end(), 0);
    }

    int getFather(int x) {
        if (father[x] != x) {
            return father[x] = getFather(father[x]);
        }
        return father[x];
    }

    void join(int src, int dest, int cost, std::vector<std::pair<int, int>> *tree) {
        int node1 = src;
        int node2 = dest;
        src = getFather(src);
        dest = getFather(dest);

        if (src != dest) {
            tree[node1].push_back({node2, cost});
            tree[node2].push_back({node1, cost});

            if (sz[src] > sz[dest]) {
                std::swap(src, dest);
            }

            father[src] = dest;
            sz[dest] += sz[src];
        }
    }
};

int n, m, q;
std::vector<edge> edges;
std::vector<std::pair<int, int>> graph[NMAX];
DSU dsu;
int ancestor[LOGMAX][NMAX], max_cost[LOGMAX][NMAX], depth[NMAX];

void computeMST() {
    dsu.init(n);
    std::sort(edges.begin(), edges.end());
    for (auto [src, dest, cost] : edges) {
        dsu.join(src, dest, cost, graph);
    }
}

void computeLCA() {
    std::function<void(int, int)> dfs = [&](int node, int parent) -> void {
        ancestor[0][node] = parent;
        depth[node] = depth[parent] + 1;
        for (auto [adj, cost] : graph[node]) {
            if (adj != parent) {
                max_cost[0][adj] = cost;
                dfs(adj, node);
            }
        }
    };

    for (int i = 1; i <= n; ++i) {
        if (!ancestor[0][i]) {
            dfs(i, 0);
        }
    }

    for (int p = 1; p <= LOG; ++p) {
        for (int i = 1; i <= n; ++i) {
            ancestor[p][i] = ancestor[p - 1][ancestor[p - 1][i]];
            max_cost[p][i] = std::max(max_cost[p - 1][i], max_cost[p - 1][ancestor[p - 1][i]]);
        }
    }
}

int getMax(int x, int y) {
    if (depth[x] < depth[y]) {
        std::swap(x, y);
    }

    int ans = -1;
    int k = depth[x] - depth[y];
    for (int p = LOG; p >= 0; --p) {
        if (k & (1 << p)) {
            ans = std::max(ans, max_cost[p][x]);
            x = ancestor[p][x];
        }
    }

    if (x == y) {
        return ans;
    }

    for (int p = LOG; p >= 0; --p) {
        if (ancestor[p][x] != ancestor[p][y]) {
            ans = std::max(ans, max_cost[p][x]);
            ans = std::max(ans, max_cost[p][y]);
            x = ancestor[p][x];
            y = ancestor[p][y];
        }
    }

    ans = std::max(ans, max_cost[0][x]);
    return ans;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    fin >> n >> m >> q;
    for (int i = 1; i <= m; ++i) {
        int x, y, c;
        fin >> x >> y >> c;
        edges.push_back({x, y, c});
    }

    computeMST();
    computeLCA();

    while (q--) {
        int x, y;
        fin >> x >> y;
        fout << getMax(x, y) << "\n";
    }

    return 0;
}