Cod sursa(job #2777113)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 22 septembrie 2021 09:59:49
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.87 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX(30005);
struct chestie
{
    int x, y, c;
} Muchii[NMAX];
int Arb[NMAX], Rank[NMAX], dp[15005][19][2], lev[15005];
bool viz[NMAX];
vector<pair<int, int> > G[NMAX];

inline bool cmp(chestie a, chestie b)
{
    return a.c < b.c;
}

int findR(int x)
{
    int val = x, y;
    while(x != Arb[x])
        x = Arb[x];
    while(val != Arb[val])
    {
        y = Arb[val];
        Arb[val] = x;
        val = y;
    }
    return x;
}

void Unite(int x, int y)
{
    if(Rank[x] > Rank[y])
        Arb[x] = Arb[y];
    else Arb[y] = Arb[x];
    if(Rank[x] == Rank[y])
        ++Rank[y];
}

void DFS(int nod, int tata)
{
    viz[nod] = 1;
    for(int i = 1; i < 18; ++i)
    {
        if(dp[nod][i - 1][0] != -1)
        {
            dp[nod][i][0] = dp[dp[nod][i - 1][0]][i - 1][0];
            dp[nod][i][1] = max(dp[nod][i - 1][1], dp[dp[nod][i - 1][0]][i - 1][1]);
        }
    }
    for(auto it: G[nod])
        if(it.first != tata)
        {
            lev[it.first] = 1 + lev[nod];
            dp[it.first][0][0] = nod;
            dp[it.first][0][1] = it.second;
            DFS(it.first, nod);
        }
}

int solve(int x, int y)
{
    if(lev[x] < lev[y])
        swap(x, y);
    int maxx = 0;
    if(lev[x] != lev[y])
    {
        for(int i = 14; i >= 0; --i)
        {
            if(lev[x] - (1 << i) >= lev[y])
            {
                maxx = max(maxx, dp[x][i][1]);
                x = dp[x][i][0];
            }
        }
    }
    if(x == y)
        return maxx;
    for(int i = 18; i >= 0; --i)
        if(dp[x][i][0] != -1 && dp[y][i][0] != -1 && dp[x][i][0] != dp[y][i][0])
        {
            maxx = max(maxx, dp[x][i][1]);
            maxx = max(maxx, dp[y][i][1]);
            x = dp[x][i][0];
            y = dp[y][i][0];
        }
    maxx = max(maxx, max(dp[x][0][1], dp[y][0][1]));
    return maxx;
}

int main()
{
    int n, m, k;
    fin >> n >> m >> k;
    for(int i = 1; i <= m; ++i)
        fin >> Muchii[i].x >> Muchii[i].y >> Muchii[i].c;
    sort(Muchii + 1, Muchii + m + 1, cmp);
    for(int i = 1; i <= n; ++i)
        Arb[i] = i;
    int nr = 0;
    vector<chestie> rez;
    for(int i = 1; i <= m && nr < n - 1; ++i)
        if(findR(Muchii[i].x) != findR(Muchii[i].y))
        {
            rez.push_back(Muchii[i]);
            Unite(findR(Muchii[i].x), findR(Muchii[i].y));
            ++nr;
        }
    for(auto it: rez)
        G[it.x].push_back({it.y, it.c}), G[it.y].push_back({it.x, it.c});
    memset(dp, -1, sizeof(dp));
    for(int i = 1; i <= n; ++i)
        if(!viz[i])
            DFS(i, 0);
    for(int i = 1; i <= k; ++i)
    {
        int x, y;
        fin >> x >> y;
        fout << solve(x, y) << '\n';
    }
    return 0;
}