Pagini recente » Cod sursa (job #1933171) | Cod sursa (job #2066854) | Cod sursa (job #1376103) | Cod sursa (job #1325108) | Cod sursa (job #2477457)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
vector < int > g[15001];
int x, y, n, m, t, k, p, r ;
int tata[15001],nivel[15001],cost[15001];
struct str
{
int x, y, c;
}v[30001];
int cmp(str a, str b)
{
return a.c<b.c;
}
int parinte(int x)
{
while(tata[x]>0)
x=tata[x];
return x;
}
void dfs(int nod)
{
for(int i=0;i<g[nod].size();i++)
{
int v=g[nod][i];
nivel[v]=nivel[nod]+1;
dfs(v);
}
}
int main()
{
fin>>n>>m>>t;
for(int i=1;i<=m;i++)
fin>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+m+1,cmp);
for(int i=1;i<=n;i++)
tata[i]=-1;
int k=0;
for(int i=1;k<n-1;i++)
{
x=parinte(v[i].x);
y=parinte(v[i].y);
if(x!=y)
{
if(tata[x]<tata[y])
{
tata[x]+=tata[y];
tata[y]=x;
g[x].push_back(y);
r=x;
cost[y]=v[i].c;
}
else
{
tata[y]+=tata[x];
tata[x]=y;
g[y].push_back(x);
r=y;
cost[x]=v[i].c;
}
k++;
}
}
nivel[r]=1;
dfs(r);
for(int i=1;i<=t;i++)
{
fin>>x>>y;
int Max=0;
while(nivel[x]>nivel[y])
{
Max=max(Max,cost[x]);
x=tata[x];
}
while(nivel[y]>nivel[x])
{
Max=max(Max,cost[y]);
y=tata[y];
}
while(x!=y)
{
Max=max(Max,cost[x]);
x=tata[x];
Max=max(Max,cost[y]);
y=tata[y];
}
fout<<Max<<'\n';
}
return 0;
}