Cod sursa(job #3338150)

Utilizator paulihno15Ciumandru Paul paulihno15 Data 31 ianuarie 2026 21:50:50
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.89 kb
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>

using namespace std;

#define NMAX 15000
#define MMAX 30000
#define PMAX 524288
#define LOG 14
#define INF_INT 2e9
#define INF_LL 1e18
#define BASE 666013
#define MOD 1000000007

#define u32 uint32_t
#define ll long long
#define ldb long double
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pipii pair<int, pair<int, int>>
#define piv pair<int, vector<int>>
#define tpl tuple<int, int, int>

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

int n, m, q, x, y;
vector<pii> arb[NMAX + 2];
vector<int> par(NMAX + 2), rng(NMAX + 2, 1), niv(NMAX + 2);
vector<vector<pii>> up(NMAX + 2, vector<pii>(LOG));

struct mc {
    int x, y, c;
}mch[MMAX + 2];

bool cmp(const mc &a, const mc &b) {
    return a.c < b.c;
}

int Find(int x) {
    if (x == par[x]) {
        return x;
    }
    return par[x] = Find(par[x]);
}

void Union(int x, int y) {
    if (rng[x] < rng[y]) {
        swap(x, y);
    }
    par[y] = x;
    rng[x] += rng[y];
}

void dfs(int nod, int par) {
    for (auto it : arb[nod]) {
        int vec = it.first;
        int cst = it.second;
        if (par == vec) {
            continue;
        }

        niv[vec] = niv[nod] + 1;
        up[vec][0] = {nod, cst};
        for (int i = 1; i < LOG; i++) {
            up[vec][i].first = up[up[vec][i - 1].first][i - 1].first;
            up[vec][i].second = max(cst, up[up[vec][i - 1].first][i - 1].second);
        }
        dfs(vec, nod);
    }
}

int lca_max(int x, int y) {
    int ans = 0;
    if (niv[x] < niv[y]) {
        swap(x, y);
    }

    for (int i = LOG - 1; i >= 0; i--) {
        if (niv[x] - (1 << i) >= niv[y]) {
            ans = max(ans, up[x][i].second);
            x = up[x][i].first;
        }
    }

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

    for (int i = LOG - 1; i >= 0; i--) {
        if (up[x][i].first && up[x][i].first != up[y][i].first) {
            ans = max(ans, up[x][i].second);
            ans = max(ans, up[y][i].second);
            x = up[x][i].first;
            y = up[y][i].first;
        }
    }
    ans = max(ans, up[x][0].second);
    ans = max(ans, up[y][0].second);
    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> n >> m >> q;

    for (int i = 1; i <= n; i++) {
        par[i] = i;
    }

    for (int i = 1; i <= m; i++) {
        fin >> mch[i].x >> mch[i].y >> mch[i].c;
    }

    sort(mch + 1, mch + m + 1, cmp);

    for (int i = 1; i <= m; i++) {
        int a = Find(mch[i].x);
        int b = Find(mch[i].y);
        if (a != b) {
            arb[mch[i].x].push_back({mch[i].y, mch[i].c});
            arb[mch[i].y].push_back({mch[i].x, mch[i].c});
            Union(a, b);
        }
    }

    dfs(1, -1);

    while (q--) {
        fin >> x >> y;
        fout << lca_max(x, y) << "\n";
    }
    return 0;
}