Cod sursa(job #1156361)

Utilizator otto1Palaga Vicentiu-Octavian otto1 Data 27 martie 2014 16:36:43
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include<fstream>
#include<vector>
#include<algorithm>
#define N 15100
#define M 30100
#define c first
#define a second.first
#define b second.second
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
int n,m,k,i,x,y,sol,t[N],niv[N],cost[N];
vector<int>v[N];
pair<int,pair<int,int> >mu[M];
inline int tata(int xx)
{
    while(t[xx]!=xx)
    xx=t[xx];
    return xx;
}
inline void uneste(int x,int y)
{
    t[x]=y;
}
inline void dfs(int x,int nivel)
{
    niv[x]=nivel;
    for(vector<int>::iterator it=v[x].begin();it!=v[x].end();++it)
    {
        dfs(*it,nivel+1);
    }
}
int main()
{
    f>>n>>m>>k;
    for(i=1;i<=m;++i)
    {
        f>>mu[i].a>>mu[i].b>>mu[i].c;
    }
    sort(mu+1,mu+m+1);
    for(i=1;i<=n;++i)
    t[i]=i;
    for(i=1;i<=m;++i)
    {
        x=tata(mu[i].a);
        y=tata(mu[i].b);
        if(x!=y)
        {
            uneste(x,y);
            v[y].push_back(x);
            cost[x]=mu[i].c;
        }
    }
    for(i=1;i<=n;++i)
    if(i==t[i])
    {
        dfs(i,0);
        break;
    }
    for(i=1;i<=k;++i)
    {
        f>>x>>y;
        sol=0;
        while(niv[x]>niv[y])
        {
            sol=max(sol,cost[x]);
            x=t[x];
        }
        while(niv[x]<niv[y])
        {
            sol=max(sol,cost[y]);
            y=t[y];
        }
        while(x!=y)
        {
            sol=max(sol,max(cost[x],cost[y]));
            x=t[x];
            y=t[y];
        }
        g<<sol<<'\n';
    }
    return 0;
}