Cod sursa(job #2695691)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 14 ianuarie 2021 11:03:19
Problema Radiatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
struct ceva
{
    int x,y,c;
} muchii[30005];
vector <int > lista[15005];
int numar[15005],a[15005],cost[15005],nivel[15005];
int S(int x)
{
    while(a[x])x=a[x];
    return x;
}
bool cmp(ceva a,ceva b)
{
    return a.c<b.c;
}
void dfs(int nod)
{
    for(int i=0; i<lista[nod].size(); i++)
    {
        int x=lista[nod][i];
        nivel[x]=nivel[nod]+1;
        dfs(x);
    }
}
int main()
{
    int n,m,i,k;
    f>>n>>m>>k;
    for(i=1; i<=m; i++)
    {
        int x,y,c;
        f>>muchii[i].x;
        f>>muchii[i].y;
        f>>muchii[i].c;
    }
    sort(muchii+1,muchii+m+1,cmp);
    for(i=1; i<=m; i++)
    {
        int x=muchii[i].x,y=muchii[i].y,c=muchii[i].c;
        int xx=S(x),yy=S(y);
        if(xx!=yy)
        {
            a[xx]=yy;
            lista[yy].push_back(xx);
            cost[xx]=c;
        }
    }
    for(i=1; i<=n; i++)
    {
        if(!a[i])
        {
            dfs(i);
        }
    }
    for(i=1; i<=k; i++)
    {
        int x,y;
        f>>x>>y;
        int Max=0;
        while(nivel[x]>nivel[y])
        {
            Max=max(Max,cost[x]);
            x=a[x];
        }
        while(nivel[y]>nivel[x])
        {
            Max=max(Max,cost[y]);
            y=a[y];
        }
        while(x!=y)
        {
            Max=max(Max,max(cost[x],cost[y]));
            x=a[x];
            y=a[y];
        }
        g<<Max<<'\n';
    }
    return 0;
}