Cod sursa(job #895449)

Utilizator fhandreiAndrei Hareza fhandrei Data 27 februarie 2013 11:29:35
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.56 kb
// Include
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
 
// Definitii
#define mp make_pair
#define pb push_back
 
#define edge pair<int, pair<int, int> >
#define cost first
#define from second.first
#define to second.second
 
#define edg pair<int, int>
 
// Constante
const int sz = (int)15e3+1;
 
// Functii
int getRoot(int node);
void dfs(int node, int lvl, int father);
#define node second
 
// Variabile
ifstream in("radiatie.in");
ofstream out("radiatie.out");
 
bool found;
int treeRoot;
int nodes, edges, questions;
 
bool visited[sz];
int father[sz], root[sz];
int level[sz], costs[sz];
 
vector<int> graph[sz];
priority_queue< edge, vector<edge>, greater<edge> > heap;
 
// Main
int main()
{
    in >> nodes >> edges >> questions;
	
    int rFrom, rTo, rCost;
    while(edges--)
    {
        in >> rFrom >> rTo >> rCost;
        heap.push(mp(rCost, mp(rFrom, rTo)));
    }
	
    while(!heap.empty())
    {
        edge current = heap.top();
        heap.pop();
		
        int node1 = current.from;
        int node2 = current.to;
		
        int root1 = getRoot(node1);
        int root2 = getRoot(node2);
		
        if(root1 == root2)
            continue;
		
        root[root2] = root1;
		
		//father[root2] = root1;
		if(father[node1])
		{
			father[node2] = node1;
			costs[node2] = current.cost;
		}
		else
		{
			father[node1] = node2;
			costs[root1] = current.cost;
		}
		
		treeRoot = root1;
		
        graph[node1].pb(node2);
		graph[node2].pb(node1);
    }
    
	dfs(treeRoot, 1, -1);
	
	int node1, node2;
    while(questions--)
    {
		in >> node1 >> node2;
		int maxEdge = 0;
		
		while(level[node1] < level[node2])
		{
			maxEdge = max(maxEdge, costs[node2]);
			node2 = father[node2];
		}
		
		while(level[node2] < level[node1])
		{
			maxEdge = max(maxEdge, costs[node1]);
			node1 = father[node1];
		}
		
		while(node1 != node2)
		{
			maxEdge = max(maxEdge, costs[node1]);
			node1 = father[node1];
			
			maxEdge = max(maxEdge, costs[node2]);
			node2 = father[node2];
		}
		
		out << maxEdge << '\n';
    }
	
    in.close();
    out.close();
    return 0;
}
 
#undef node
#undef to
#define to second
void dfs(int node, int lvl, int father)
{
	level[node] = lvl;
	vector<int>::iterator it, end=graph[node].end();
	for(it=graph[node].begin() ; it!=end ; ++it)
		if(*it != father)
			dfs(*it, lvl+1, node);
}
 
 
int getRoot(int node)
{   return root[node]? root[node]=getRoot(root[node]) : node;   }