Cod sursa(job #3153136)

Utilizator Luka77Anastase Luca George Luka77 Data 28 septembrie 2023 13:09:57
Problema Radiatie Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.61 kb
#include <bits/stdc++.h>
using namespace std;

/// INPUT / OUTPUT
const string problem = "radiatie";
ifstream fin(problem + ".in");
ofstream fout(problem + ".out");

struct edge
{
    int x, y, cost;
};
 
struct DSU
{
    vector<int>sz, father;
    DSU(int n)
    {
        sz.resize(n + 1);
        father.resize(n + 1);
        for(int i = 1; i <= n; ++ i)
        {
            father[i] = i;
            sz[i] = 1;
        }
    }
    
    inline int FindFather(int x)
    {
        if(x == father[x])
            return x;
        else
            return father[x] = FindFather(father[x]);
    }
    
    inline bool Same_Father(int x, int y)
    {
        return FindFather(x) == FindFather(y);
    }
    
    inline void Join(int x, int y)
    {
        int father_x = FindFather(x), father_y = FindFather(y);
        
        if(father_x == father_y)
            return;
        
        if(sz[father_x] > sz[father_y]) // Father_y va fi mereu mai mare
            swap(father_x, father_y);
        
        sz[father_y] += sz[father_x];
        father[father_x] = father_y;
    }
};

const int NMAX = 15005;
int nodes, edges, queries;
bool viz[NMAX];
int price[NMAX], maxi[NMAX];
edge graph[NMAX];
vector<pair<int,int>>tree[NMAX];
DSU pdm(NMAX);

inline bool custom(const edge &x, const edge &y)
{
    return x.cost < y.cost;
}

inline void CreateAPM()
{
    for(int i = 1; i <= nodes; ++ i)
    {
        if(!pdm.Same_Father(graph[i].x, graph[i].y))
        {
            tree[graph[i].x].push_back({graph[i].y, graph[i].cost});
            tree[graph[i].y].push_back({graph[i].x, graph[i].cost});
            pdm.Join(graph[i].x, graph[i].y);
        }
    }
}

inline void DFS(int start)
{
    if(viz[start])
        return;
    viz[start] = 1;
    
    for(auto new_node : tree[start])
    {
        if(!viz[new_node.first])
        {
            maxi[new_node.first] = max(maxi[start], new_node.second);
            price[new_node.first] = price[start] + new_node.second;
            DFS(new_node.first);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);
    
    fin >> nodes >> edges >> queries;
    
    for(int i = 1; i <= edges; ++ i)
    {
        int node_1, node_2, cost;
        fin >> node_1 >> node_2 >> cost;
        graph[i].x = node_1; graph[i].y = node_2; graph[i].cost = cost;
    }
    
    sort(graph + 1, graph + nodes + 1, custom);
    
    CreateAPM();

    
    while(queries--)
    {
        int a, b;
        fin >> a >> b;
        DFS(a);
        fout << maxi[b] << '\n';
        memset(viz, 0, sizeof(viz));
        memset(maxi, 0, sizeof(maxi));
    }
    
    return 0;
}