Pagini recente » Cod sursa (job #2580845) | Cod sursa (job #1817433) | Cod sursa (job #1642676) | Cod sursa (job #2037889) | Cod sursa (job #2686578)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
struct edg{
int a, b, v;
bool operator<(const edg &rhs){
return v < rhs.v;
}
};
const int N = 15041;
int n, k, m;
vector<edg> edgy;
int tre[N], wei[N], val[N];
int dad(int a){
while(a != tre[a]){
a = tre[a];
}
return a;
}
void meuni(int a, int b, int v){
a = dad(a); b = dad(b);
if(wei[a] >= wei[b]){
tre[b] = a;
val[b] = v;
wei[a] += wei[b];
}else{
tre[a] = b;
val[a] = b;
wei[b] += wei[a];
}
}
bool meche(int a, int b){
return dad(a) == dad(b);
}
void cuscra(){
for(int i = 1; i <= n; ++i){
tre[i] = i;
}
sort(edgy.begin(), edgy.end());
for(auto ed : edgy){
if(!meche(ed.a, ed.b)){
meuni(ed.a, ed.b, ed.v);
}
}
}
int dep[N];
int deaptha(int a){
if(dep[a] == 0){
if(a == tre[a]){
dep[a] = 1;
}else{
dep[a] = deaptha(tre[a])+1;
}
}
return dep[a];
}
void johnny_depth(){
for(int i = 1; i <= n; ++i){
if(dep[i] == 0)deaptha(i);
}
}
int equalize(int &a, int &b){
int maxi = 0;
int &hi = (dep[a]>dep[b] ? a : b);
int &lo = (dep[a]<=dep[b] ? a : b);
while(dep[hi] > dep[lo]){
maxi = max(maxi, val[hi]);
hi = tre[hi];
}
return maxi;
}
int calc(int a, int b){
int maxi = equalize(a, b);
while(a != b){
maxi = max(maxi, val[a]);
maxi = max(maxi, val[b]);
a = tre[a];
b = tre[b];
}
return maxi;
}
int main(){
// ios_base::sync_with_stdio(false);
fin >> n >> k >> m;
for(int i = 0; i < k; ++i){
edg ed;
fin >> ed.a >> ed.b >> ed.v;
edgy.push_back(ed);
}
cuscra();
johnny_depth();
for(int i = 0; i < m; ++i){
int a, b;
fin >> a >> b;
fout << calc(a, b) << "\n";
}
return 0;
}