Cod sursa(job #2150586)

Utilizator usureluflorianUsurelu Florian-Robert usureluflorian Data 3 martie 2018 17:27:55
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f ("radiatie.in");
ofstream g ("radiatie.out");
const int nmax=3e4+3;
struct usu
{
      int x,y,v;
}mu[nmax];
int n,m,k,t[nmax],lv[nmax],c[nmax],x,y,mx;
vector <int> mc[nmax];
bool cmp(usu a,usu b) {return a.v<b.v;}
int root(int x)
{
    while(t[x]) x=t[x];
    return x;
}
void dfs(int nod)
{
    for(int i=0;i<mc[nod].size();++i)
    {
        lv[mc[nod][i]]=lv[nod]+1;
        dfs(mc[nod][i]);
    }
}
int main()
{
    f>>n>>m>>k;
    for(int i=1;i<=m;++i) f>>mu[i].x>>mu[i].y>>mu[i].v;
    sort(mu+1,mu+m+1,cmp);
    for(int i=1;i<=m;++i)
    {
        x=root(mu[i].x);
        y=root(mu[i].y);
        if(x!=y)
        {
            t[x]=y;
            mc[y].push_back(x);
            c[x]=mu[i].v;
        }
    }
    int tp=0;
    for(tp=1;t[tp];++tp);
    dfs(tp);
    while(k--)
    {
        f>>x>>y;
        mx=0;
        if(lv[x]<lv[y]) swap(x,y);
        while(lv[x]>lv[y])
        {
            mx=max(mx,c[x]);
            x=t[x];
        }
        while(x!=y)
        {
            mx=max(mx,max(c[x],c[y]));
            x=t[x];
            y=t[y];
        }
        g<<mx<<'\n';
    }
    return 0;
}