Pagini recente » Cod sursa (job #58241) | Cod sursa (job #2973550) | Cod sursa (job #756373) | Cod sursa (job #519534) | Cod sursa (job #1160188)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct edge
{
int x,y,v;
}e[30001];
int n,m,k,t[15001],lv[15001],c[15001];
vector<int> g[15001];
bool cmp(edge a,edge b)
{
return a.v<b.v;
}
int root(int x)
{
while(t[x])
x=t[x];
return x;
}
void dfs(int nod)
{
for(size_t i=0;i<g[nod].size();++i)
{
int vecin=g[nod][i];
lv[vecin]=lv[nod]+1;
dfs(vecin);
}
}
int query(int x,int y)
{
int mx=0;
while(lv[x]<lv[y])
{
mx=max(mx,c[y]);
y=t[y];
}
while(lv[x]>lv[y])
{
mx=max(mx,c[x]);
x=t[x];
}
while(x!=y)
{
mx=max(mx,max(c[x],c[y]));
x=t[x];
y=t[y];
}
return mx;
}
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int main()
{
int i,x,y;
fin>>n>>m>>k;
for(i=0;i<m;++i)
fin>>e[i].x>>e[i].y>>e[i].v;
sort(e,e+m,cmp);
for(i=0;i<m;++i)
{
x=root(e[i].x);
y=root(e[i].y);
if(x!=y)
{
t[x]=y;
g[y].push_back(x);
c[x]=e[i].v;
}
}
for(i=1;t[i];++i);
dfs(i);
while(k--)
{
fin>>x>>y;
fout<<query(x,y)<<"\n";
}
return 0;
}