Cod sursa(job #3172233)

Utilizator TeodoraMaria123Serban Teodora Maria TeodoraMaria123 Data 20 noiembrie 2023 12:48:21
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.16 kb
#include <bits/stdc++.h>

using namespace std;

#define MAX_N 15000

struct edge
{
    int a, b, cost;
};

int n, m, k, timer, L;
vector <int> parent, height, tin, tout;
vector <edge> edges;
vector <vector <pair <int, int> > > graph, dp;
bitset <MAX_N + 1> viz;

int findParent(int x)
{
    if(x == parent[x])
        return x;
    return parent[x] = findParent(parent[x]);
}

void mergeSets(int nodeA, int nodeB, int edgeCost)
{
    int a = findParent(nodeA);
    int b = findParent(nodeB);

    if(a == b)
        return;

    graph[nodeA].push_back({nodeB, edgeCost});
    graph[nodeB].push_back({nodeA, edgeCost});

    if(height[a] < height[b])
        swap(a, b);
    parent[b] = a;
    if(height[a] == height[b])
        height[a] ++;
}

void dfs(int node, int parent, int edgeCost)
{
    viz[node] = 1;
    tin[node] = ++timer;
    height[node] = height[parent] + 1;

    dp[0][node].first = parent;
    dp[0][node].second = edgeCost;
    for(int i = 1; i <= L; i ++)
    {
        dp[i][node].first = dp[i - 1][dp[i - 1][node].first].first;
        dp[i][node].second = max(dp[i - 1][node].second, dp[i - 1][dp[i - 1][node].first].second);
    }

    for(pair <int, int> neighbour : graph[node])
        if(!viz[neighbour.first])
            dfs(neighbour.first, node, neighbour.second);

    tout[node] = ++timer;
}

inline bool isAncestor(int a, int b)
{
    return (tin[a] <= tin[b]  &&  tout[b] <= tout[a]);
}

int lca(int a, int b)
{
    if(isAncestor(a, b))
        return a;
    if(isAncestor(b, a))
        return b;

    for(int i = L; i >= 0; i --)
    {
        if(!isAncestor(dp[i][a].first, b))
            a = dp[i][a].first;
    }
    return dp[0][a].first;
}

void solve(int a, int b)
{
    int centru = lca(a, b), ans = -1;

    for(int i = L; i >= 0; i --)
    {
        if(height[dp[i][a].first] >= height[centru])
        {
            ans = max(ans, dp[i][a].second);
            a = dp[i][a].first;
        }
        if(height[dp[i][b].first] >= height[centru])
        {
            ans = max(ans, dp[i][b].second);
            b = dp[i][b].first;
        }
    }
    cout << ans << "\n";
}

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    freopen("raditie.in", "r", stdin);
    freopen("radiatie.out", "w", stdout);

    cin >> n >> m >> k;

    L = log2(n);
    graph.resize(n + 1);
    height.resize(n + 1, 1);
    parent.resize(n + 1);
    for(int i = 1; i <= n; i ++)
        parent[i] = i;
    dp.resize(L + 1, vector <pair <int, int> > (n + 1)); // ce nu i place kb3 ??

    for(int i = 0; i < m; i ++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        edges.push_back({a, b, c});
    }

    sort(edges.begin(), edges.end(), [](const edge &a, const edge &b)
    {
        return a.cost < b.cost;
    });

    for(int i = 0; i < m; i ++)
    {
        mergeSets(edges[i].a, edges[i].b, edges[i].cost);
    }

    height.clear();
    height.resize(n + 1);// sunt bune?

    dfs(1, 1, -1);

    for(int i = 0; i < k; i ++)
    {
        int a, b;
        cin >> a >> b;
        solve(a, b);
    }
    return 0;
}