Cod sursa(job #3353037)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 3 mai 2026 20:06:14
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda cerc-acs-02-05-26 Marime 2.76 kb
#include <bits/stdc++.h>  

using namespace std; 

const int max_size = 3e4 + 20, INF = 1e9 + 7, rmax = 15;

struct str{
    int x, y, c;
    bool operator < (const str &aux) const
    {
        return c < aux.c;
    }
};

vector <str> muchii;
vector <pair <int, int>> mc[max_size];
int t[max_size], bl[rmax][max_size], blmx[rmax][max_size], lvl[max_size], lg[max_size], ans;

int rad (int x)
{
    if (t[x] == x)
    {
        return x;
    }
    return t[x] = rad(t[x]);
}

void dfs (int nod, int par, int cost)
{
    bl[0][nod] = par;
    blmx[0][nod] = cost;
    for (int e = 1; e < rmax; e++)
    {
        bl[e][nod] = bl[e - 1][bl[e - 1][nod]];
        blmx[e][nod] = max(blmx[e - 1][nod], blmx[e - 1][bl[e - 1][nod]]);
    }
    for (auto f : mc[nod])
    {
        if (f.first != par)
        {
            lvl[f.first] = lvl[nod] + 1;
            dfs(f.first, nod, f.second);
        }
    }
}

int anc (int nod, int dx)
{
    int e = 0;
    while (dx > 0)
    {
        if (dx % 2 == 1)
        {
            ans = max(ans, blmx[e][nod]);
            nod = bl[e][nod];
        }
        dx /= 2;
        e++;
    }
    return nod;
}

void lca (int x, int y)
{
    int e = rmax - 1;
    while (e >= 0)
    {
        if (bl[e][x] != bl[e][y])
        {
            ans = max(ans, blmx[e][x]);
            ans = max(ans, blmx[e][y]);
            x = bl[e][x];
            y = bl[e][y];
        }
        e--;
    }
    ans = max(ans, blmx[0][x]);
    ans = max(ans, blmx[0][y]);
}

void solve()
{
    int n, m, q;
    cin >> n >> m >> q;
    while (m--)
    {
        int x, y, c;
        cin >> x >> y >> c;
        muchii.push_back({x, y, c});
    }
    sort(muchii.begin(), muchii.end());
    for (int i = 1; i <= n; i++)
    {
        t[i] = i;
    }
    for (auto f : muchii)
    {
        int rx = rad(f.x), ry = rad(f.y);
        if (rx != ry)
        {
            t[rx] = ry;
            mc[f.x].push_back({f.y, f.c});
            mc[f.y].push_back({f.x, f.c});
        }
    }
    dfs(1, 0, 0);
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        if (lvl[x] > lvl[y])
        {
            swap(x, y);
        }
        ans = 0;
        y = anc(y, lvl[y] - lvl[x]);
        if (x != y)
        {
            lca(x, y);
        }
        cout << ans << "\n";
    }
}

signed main() 
{ 
#ifdef LOCAL 
    freopen("test.in", "r", stdin); 
    freopen("test.out", "w", stdout);
#else
    freopen("radiatie.in", "r", stdin);
    freopen("radiatie.out", "w", stdout);
#endif // LOCAL 
    ios_base::sync_with_stdio(false); 
    cin.tie(0); 
    cout.tie(0); 
    long long tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0; 
}