Pagini recente » Cod sursa (job #1315380) | Cod sursa (job #1784858) | Cod sursa (job #374712) | Cod sursa (job #3244233) | Cod sursa (job #2504480)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
const int nmax=15005;
vector<pair<int,pair<int,int>>>l;
int tata[nmax],d[nmax],cm[nmax];
int supreme(int a)
{
int x=a;
while(x!=tata[x])
x=tata[x];
return x;
}
void unite(int x,int y,int co)
{
int tx=supreme(x),ty=supreme(y);
if(d[tx]==d[ty])
{
d[tx]++;
tata[ty]=tx;
cm[ty]=co;
}
else
if(d[tx]>d[ty])
{
tata[ty]=tx;
cm[ty]=co;
}
else
{
tata[tx]=ty;
cm[tx]=co;
}
}
bool check(int x,int y)
{
int tx=supreme(x),ty=supreme(y);
return (tx==ty);
}
int solve(int a,int b)
{
int cost=0;
while(a!=b){
if(d[a]<d[b])
{
cost=max(cost,cm[a]);
a=tata[a];
}
else
{
cost=max(cost,cm[b]);
b=tata[b];
}}
return cost;
}
int main()
{
ifstream cin("radiatie.in");
ofstream cout("radiatie.out");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,k,x,y,c,a,b;
cin>>n>>m>>k;
for(int i=1; i<=m; i++)
{
cin>>x>>y>>c;
l.push_back({c,{x,y}});
}
sort(l.begin(),l.end());
for(int i=1; i<=n; i++)
tata[i]=i;
for(auto x:l)
if(check(x.second.first,x.second.second)==false)
unite(x.second.first,x.second.second,x.first);
while(k--)
{
cin>>a>>b;
cout<<solve(a,b)<<"\n";
}
return 0;
}