Cod sursa(job #1517872)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 4 noiembrie 2015 22:37:32
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.75 kb
#include <fstream>
#include <vector>
#include <algorithm>

#define NMax 15010

using namespace std;

ifstream f("radiatie.in");
ofstream g("radiatie.out");

using namespace std;

int nodes, edges, queries, disTree[NMax], root;
vector< pair<int, int> > apm[NMax];
bool mark[NMax];

struct edge {
    int ext1;
    int ext2;
    int cost;
} G[30010];

struct mnode {
    int level;
    int father;
    int cost;
}arbStruct[NMax];

bool cmp(const edge &e1, const edge &e2)
{
    return e1.cost < e2.cost;
}

void DFS(int node, int level)
{
    mark[node] = true;
    for (vector<pair<int, int>>::iterator it = apm[node].begin(); it != apm[node].end(); it++) {
        if (!mark[it->first]) {
            arbStruct[it->first].level = level;
            arbStruct[it->first].father = node;
            arbStruct[it->first].cost = it->second;
            
            DFS(it->first, level+1);
        }
    }
}

int findFather(int node)
{
    while (disTree[node] > 0)
        node = disTree[node];
    
    return node;
}

int getMax(int a, int b)
{
    if (a > b)
        return a;
    return b;
}

int main()
{
    f>>nodes>>edges>>queries;
    
    for (int i=1; i<=edges; i++)
        f>>G[i].ext1>>G[i].ext2>>G[i].cost;
    for (int i=1; i<=nodes; i++)
        disTree[i] = -1;
    
    sort (G+1, G+edges+1, cmp);
    
    for (int i=1; i<=nodes-1; i++)
    {
        int father1 = findFather(G[i].ext1);
        int father2 = findFather(G[i].ext2);
        
        if (father1 != father2) {
            if (-father1 > -father2) {
                disTree[father1] += disTree[father2];
                disTree[father2] = father1;
                
                root = father1;
            }
            else {
                disTree[father2] += disTree[father1];
                disTree[father1] = father2;
                
                root = father2;
            }
            
            apm[G[i].ext1].push_back( make_pair(G[i].ext2, G[i].cost));
            apm[G[i].ext2].push_back( make_pair(G[i].ext1, G[i].cost));
        }
    }
    
    DFS(root, 1);
    
    int x, y;
    for (int i=1; i<=queries; i++) {
        f>>x>>y;
        
        int answ = -1;
        while (arbStruct[x].level > arbStruct[y].level) {
            answ = getMax(answ, arbStruct[x].cost);
            
            x = arbStruct[x].father;
        }
        while (arbStruct[x].level < arbStruct[y].level) {
            answ = getMax(answ, arbStruct[y].cost);
            
            y = arbStruct[y].father;
        }
        
        while (x != y) {
            answ = getMax(answ, arbStruct[x].cost);
            answ = getMax(answ, arbStruct[y].cost);
            
            x = arbStruct[x].father;
            y = arbStruct[y].father;
        }
        
        g << answ << "\n";
    }
    
    return 0;
}