Cod sursa(job #3307560)

Utilizator CosminaneBoac Mihai Cosmin Cosminane Data 21 august 2025 16:40:03
Problema Radiatie Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
struct muchie{
	int x, y, c;
};
struct elem{
	int x, c;
};
struct comp{
	bool operator()( muchie a, muchie b ){
		return a.c > b.c;
	}
};
priority_queue <muchie, vector <muchie>, comp> q;
vector <muchie> v[15005];
int f[15005], h[15005];
elem rmq[15005][15];
int main(){
	int n, m, k, i, j, x, y, c, ras;
	ifstream fin( "radiatie.in" );
	ofstream fout( "radiatie.out" );
	fin >> n >> m >> k;
	for( i = 0; i < m; i++ ){
		fin >> x >> y >> c;
		v[x].push_back( { x, y, c } );
		v[y].push_back( { y, x, c } );
	}
	for( i = 0; i < v[1].size(); i++ ){
		q.push( v[1][i] );
	}
	f[1] = 1;
	rmq[1][0] = { -1, -1 };
	for( i = 1; i < n; i++ ){
		while( f[q.top().y] == 1 ){
			q.pop();
		}
		rmq[q.top().y][0] = { q.top().x, q.top().c };
		h[q.top().y] = h[q.top().x] + 1;
		x = q.top().y;
		q.pop();
		f[x] = 1;
		for( j = 0; j < v[x].size(); j++ ){
			if( f[v[x][j].y] == 0 ){
				q.push( v[x][j] );
			}
		}
	}
	for( i = 1; ( 1 << i ) <= n; i++ ){
		for( j = 1; j <= n; j++ ){
			rmq[j][i] = { rmq[rmq[j][i - 1].x][i - 1].x, max( rmq[j][i - 1].c, rmq[rmq[j][i - 1].x][i - 1].c ) };
		}
	}
	for( i = 0; i < k; i++ ){
		fin >> x >> y;
		if( h[x] < h[y] ){
			swap( x, y );
		}
		ras = 0;
		for( j = 14; j >= 0; j-- ){
			if( h[x] - ( 1 << j ) >= h[y] ){
				ras = max( rmq[x][j].c, ras );
				x = rmq[x][j].x;
			}
		}
		if( x != y ){
			for( j = 14; j >= 0; j-- ){
				if( rmq[x][j].x != rmq[y][j].x ){
					ras = max( max( rmq[x][j].c, rmq[y][j].c ), ras );
					x = rmq[x][j].x;
					y = rmq[y][j].x;
				}
			}
			ras = max( max( rmq[x][0].c, rmq[y][0].c ), ras );
		}
		fout << ras << '\n';
	}
	return 0;
}