Cod sursa(job #1771693)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 5 octombrie 2016 21:34:22
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.77 kb
#include <bits/stdc++.h>


using namespace std;

ifstream fin("radiatie.in");
ofstream fout("radiatie.out");

int radicalN;
int ind, N, M, K, x, y, nr;
char c;
int niv[30010], T[30010];
pair<int, int> T2[30010], T3[30010];
vector< pair<int, int> > L[15010];
queue <int> Q;
bitset<15010>viz;

inline int get_nr()
{
    nr = 0;

    c = getc(stdin);

    while(!isdigit(c))
    {
        c = getc(stdin);
    }

    while (isdigit(c))
    {
        nr = nr * 10 + c - '0';
        c = getc(stdin);
    }


  return nr;
}

struct muchie
{
    int a, b;
    int cost;
} v[30010];

inline void queueClear( queue<int> &Q)
{
    queue<int> Empty;
    swap(Q, Empty);
}

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

inline void join(int x, int y)
{
    while(T[x] > 0)
    {
        Q.push(x);
        x = T[x];
    }
    while(T[y] > 0)
    {
        Q.push(y);
        y = T[y];
    }

    if(x == y)
    {
        queueClear(Q);
        return;
    }

    if(T[x] <= T[y])
    {
        T[x] += T[y];
        T[y] = x;

        while(!Q.empty())
        {
            T[Q.front()] = x;
            Q.pop();
        }
    }
    else
    {
        T[y] += T[x];
        T[x] = y;

        while(!Q.empty())
        {
            T[Q.front()] = y;
            Q.pop();
        }
    }

    L[ v[ind].a ].push_back(make_pair(v[ind].b, v[ind].cost));
    L[ v[ind].b ].push_back(make_pair(v[ind].a, v[ind].cost));

}

inline void dfs(int node, int n1, int level)
{
    viz[node] = 1;
    niv[node] = level;

    if(level % radicalN == 0) n1 = node;

    for(int i = 0; i < L[node].size(); i ++)
    {
        if(viz[ L[node][i].first ] == 0 )
        {
            T2[ L[node][i].first ] = make_pair(node, L[node][i].second);
            T3[ L[node][i].first ].first = n1;
            T3[ L[node][i].first ].second = max(T3[ L[node][i].first ].second, L[node][i].second);

            dfs(L[node][i].first, n1, level + 1);
        }
    }
}

int main()
{
    freopen("radiatie.in","r",stdin);

    //fin >> N >> M >> K;
    N = get_nr();
    M = get_nr();
    K = get_nr();

    radicalN = sqrt(N);

    for(int i = 1; i <= M; i ++)
    {
        //fin >> v[i].a >> v[i].b  >> v[i].cost;
        v[i].a = get_nr();
        v[i].b = get_nr();
        v[i].cost = get_nr();
    }

    sort(v + 1, v + M + 1, cmp);

    for(int i = 1; i <= N; i ++)
    {
        T[i] = -1;
    }

    for(ind = 1; ind <= M; ind ++)
    {
        join(v[ind].a, v[ind]. b);
    }

    dfs(1, 0, 0);

    for(int i = 1; i <= K; i ++)
    {
        //fin >> x >> y;
        x = get_nr();
        y = get_nr();

        long long solution = 0;

        while(T3[x].first != T3[y].first)
        {
            if(niv[x] > niv[y])
            {
                if(T3[x].second > solution)
                {
                    solution = T3[x].second;
                }
                x = T3[x].first;
            }
            else
            {
                if(T3[y].second > solution)
                {
                    solution = T3[y].second;
                }
                y = T3[y].first;
            }
        }
        while(x != y)
        {
            if(niv[x] > niv[y])
            {
                if(T2[x].second > solution)
                {
                    solution = T2[x].second;
                }
                x = T2[x].first;
            }
            else
            {
                if(T2[y].second > solution)
                {
                    solution = T2[y].second;
                }
                y = T2[y].first;
            }
        }

        fout << solution << '\n';

    }

    return 0;
}