Cod sursa(job #1016312)

Utilizator narcis_vsGemene Narcis - Gabriel narcis_vs Data 26 octombrie 2013 00:50:28
Problema Radiatie Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <fstream>
#include <algorithm>
#include <vector>

#define In "radiatie.in"
#define Out "radiatie.out"
#define Nmax 15004
#define Mmax 30004
using namespace std;
struct edge
{
    int x ,y, cost;
    bool operator <(const edge A)const
    {
        return cost < A.cost;
    }
};
edge e[Mmax];
int n, m, niv[Nmax] ,c[Nmax];
vector < int > Tree[Nmax];
int Father[Nmax];
inline int Find(int node)
{
    while(node!=Father[node])
        node = Father[node];
    return node;
}
inline void APM()
{
    sort(e+1,e+m+1);
    int i ,X ,Y;
    for(i = 1;i <= n; ++i)
        Father[i] = i;
    for(i = 1;i <= n; ++i)
    {
        X = Find(e[i].x) ;
        Y = Find(e[i].y) ;
        if(X != Y)
        {
            Father[X] = Y;
           c[X] = e[i].cost;
        }
    }
}
inline void Niv(const int X)
{
    if(Father[X]==X)
        return ;
    Niv(Father[X]);
    niv[X] = niv[Father[X]]+1;
}
int main()
{
    int i, k, sol ,X ,Y;
    ifstream f(In);
    ofstream g(Out);
    f >> n >> m >> k;
    for(i = 1 ; i <= m; ++i)
        f >> e[i].x >> e[i].y >> e[i].cost;
    APM();
    for(i = 1;i <= n; ++i)
        if(!niv[i])
            Niv(i);
    for( ;  k; --k)
    {
        f >> X >> Y ;
        sol = 0;
        while(niv[X] > niv[Y])
        {
            sol = max(sol,c[X]);
            X = Father[X];
        }
        while(niv[X] < niv[Y])
        {
            sol = max(sol,c[Y]);
            Y = Father[Y];
        }
        while(X != Y)
        {
            sol = max(sol,max(c[X],c[Y]));
            X = Father[X];
            Y = Father[Y];
        }
        g<<sol<<"\n";
    }
    f.close();
    g.close();
    return 0;
}