Pagini recente » Cod sursa (job #1794421) | Cod sursa (job #3235879) | Cod sursa (job #2007311) | Cod sursa (job #2664584) | Cod sursa (job #2477456)
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#define DIM 15000 + 1
using namespace std;
ifstream f ("radiatie.in");
ofstream g ("radiatie.out");
vector < int > graph[DIM];
struct meme{
int x, y, cost;
}v[2 * DIM];
int level[DIM], father[DIM], Cost[DIM];
int cmp ( meme a, meme b ){
return a.cost < b.cost;
}
void dfs ( int node, int lvl ){
level[node] = lvl;
for ( int i = 0; i < graph[node].size(); i++ ){
int new_node = graph[node][i];
dfs ( new_node, lvl + 1 );
}
}
int find_cost ( int x, int y ){
int MAX_cost = 0;
while ( level[x] != level[y] ){
MAX_cost = max ( MAX_cost, Cost[x] );
x = father[x];
}
while ( x != y ){
MAX_cost = max ( MAX_cost, Cost[x] );
x = father[x];
MAX_cost = max ( MAX_cost, Cost[y] );
y = father[y];
}
return MAX_cost;
}
int papa ( int x ){
while ( father[x] > 0 )
x = father[x];
return x;
}
int main()
{ int n, m, k, x, y;
f >> n >> m >> k;
for ( int i = 1; i <= m; i++ )
f >> v[i].x >> v[i].y >> v[i].cost;
sort ( v + 1, v + m + 1, cmp );
for ( int i = 1; i <= n; i++ )
father[i] = -1;
int selected = 0, i = 1, origin;
while ( selected != n - 1 ){
int x = papa ( v[i].x );
int y = papa ( v[i].y );
if ( x != y ){
if( father[x] > father[y] ){
father[y] += father[x];
father[x] = y;
Cost[x] = v[i].cost;
graph[x].push_back ( y );
origin = x;
}
else{
father[x] += father[y];
father[y] = x;
Cost[y] = v[i].cost;
graph[y].push_back ( x );
origin = y;
}
i++;
selected++;
}
}
dfs ( origin, 0 );
for ( int i = 1; i <= k; i++ ){
f >> x >> y;
if ( level[x] >= level[y] )
swap ( x, y );
g << find_cost( x, y ) << '\n';
}
return 0;
}