Pagini recente » Cod sursa (job #389270) | Cod sursa (job #2404622) | Cod sursa (job #3228684) | Cod sursa (job #1494759) | Cod sursa (job #3195369)
#include <bits/stdc++.h>
#define DIM 100001
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n, m, q, a, b, c, root, ans;
int i, j;
int tata[DIM], parcurs[DIM], depth[DIM], dp[DIM];
struct elem{
int x, y, cost;
};
vector <elem> G;
vector <pair <int, int> > G1[DIM];
bool comp(elem a, elem b){
return a.cost<b.cost;
}
int get_root(int nod){
while(tata[nod]>0)
nod=tata[nod];
return nod;
}
void join(int nod1, int nod2){
int root1=get_root(nod1);
int root2=get_root(nod2);
if(root1!=root2){
if(tata[root1]<tata[root2]){
tata[root1]+=tata[root2];
tata[root2]=root1;
root=root1;
}else{
tata[root2]+=tata[root1];
tata[root1]=root2;
root=root2;
}
}
}
void dfs(int nod, int val){
parcurs[nod]=1;
depth[nod]=val;
for(int i=0; i<G1[nod].size(); i++){
int fiu=G1[nod][i].first;
int cost=G1[nod][i].second;
if(!parcurs[fiu]){
dp[fiu]=cost;
dfs(fiu, val+1);
}
}
}
int main(){
fin>>n>>m>>q;
for(i=1; i<=m; i++){
fin>>a>>b>>c;
G.push_back({a, b, c});
}
for(i=1; i<=n; i++)
tata[i]=-1;
sort(G.begin(), G.end(), comp);
for(auto muchie:G){
if(get_root(muchie.x)!=get_root(muchie.y)){
join(muchie.x, muchie.y);
G1[muchie.x].push_back(make_pair(muchie.y, muchie.cost));
G1[muchie.y].push_back(make_pair(muchie.x, muchie.cost));
}
}
dfs(root, 0);
while(q--){
fin>>a>>b;
ans=-1000000;
while(depth[a]>depth[b]){
ans=max(ans, dp[a]);
a=tata[a];
}
while(depth[a]<depth[b]){
ans=max(ans, dp[b]);
b=tata[b];
}
while(a!=b){
ans=max(max(dp[a], dp[b]), ans),
a=tata[a];
b=tata[b];
}
fout<<ans<<"\n";
}
}