Pagini recente » Cod sursa (job #600001) | Cod sursa (job #1512915) | Cod sursa (job #232673) | Cod sursa (job #2858358) | Cod sursa (job #3183290)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n,m,k,tata[15001],cost[15001],radacina,nivel[20001];
struct numar
{
int x,y,cost;
}v[20001];
vector <int> A[20001];
int cmp(numar a,numar b)
{
return a.cost<b.cost;
}
int root(int x)
{
while(tata[x]>0)
{
x=tata[x];
}
return x;
}
void dfs(int nod,int lvl)
{
nivel[nod]=lvl;
for(auto i : A[nod])
{
dfs(i,lvl+1);
}
}
int main()
{
fin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
fin>>v[i].x>>v[i].y>>v[i].cost;
}
sort(v+1,v+n+1,cmp);
for(int i=1;i<=n;i++)
tata[i]=-1;
int p=0;
for(int i=1;p<n-1;i++)
{
int rx=root(v[i].x);
int ry=root(v[i].y);
if(rx!=ry)
{
p++;
if(tata[rx]<tata[ry])
{
A[rx].push_back(ry);
cost[ry]=v[i].cost;
tata[rx]+=tata[ry];
tata[ry]=rx;
radacina=ry;
}
else
{
A[ry].push_back(rx);
cost[rx]=v[i].cost;
tata[ry]+=tata[rx];
tata[rx]=ry;
radacina=rx;
}
}
}
dfs(radacina,1);
for(int i=1;i<=k;i++)
{
int x,y;
fin>>x>>y;
int max0=-10000000;
while(nivel[x]>nivel[y])
{
max0=max(max0,cost[x]);
x=tata[x];
}
while(nivel[y]>nivel[x])
{
max0=max(max0,cost[y]);
y=tata[y];
}
max0=max(max0,max(cost[x],cost[y]));
fout<<max0<<"\n";
}
return 0;
}