Pagini recente » Cod sursa (job #4403) | Cod sursa (job #3126557) | Cod sursa (job #2376415) | Cod sursa (job #1901973) | Cod sursa (job #2138437)
#include <bits/stdc++.h>
#define Nmax 15002
using namespace std;
ifstream f ("radiatie.in");
ofstream g ("radiatie.out");
struct filip{
int x,y,c;
}apm[2*Nmax];
int cost[Nmax],nivel[Nmax],dad[Nmax],i,j,xx,yy,Min,n,m,t,l[Nmax] , poz[2*Nmax] ,costt;
vector < pair < int,int > > v[Nmax];
bool cmp(int a,int b)
{
return apm[a].c < apm[b].c;
}
inline void bfs(int nod,int niv)
{
int l = v[nod].size(), new_nod;
for(int i = 0;i < l;i++)
{
new_nod = v[nod][i].first;
if(nivel[new_nod] == 0)
{
nivel[new_nod] = niv + 1;
dad[new_nod] = nod;
cost[new_nod] = v[nod][i].second;
bfs(new_nod, niv + 1);
}
}
}
int grupa(int x)
{
if(l[x] == x)
return x;
l[x] = grupa(l[x]);
return l[x];
}
inline void uneste(int x,int y)
{
l[grupa(x)] = grupa(y);
}
int main()
{
f >> n >> m >> t;
for(i = 1;i <= m;i++)
f >> apm[i].x >> apm[i].y >> apm[i].c , poz[i] = i;
for(i = 1;i <= n;i++)
l[i] = i;
sort(poz + 1,poz + m + 1,cmp);
for(i = 1;i <= m;i++)
{
xx = apm[poz[i]].x;
yy = apm[poz[i]].y;
costt = apm[poz[i]].c;
if(grupa(xx) != grupa(yy))
{
uneste(grupa(xx),grupa(yy));
v[xx].push_back({yy,costt});
v[yy].push_back({xx,costt});
}
}
nivel[1] = 1;
bfs(1 , 1);
for(i = 1;i <= t;i++)
{
f >> xx >> yy;
Min = 0;
if(nivel[xx] < nivel[yy])
swap(xx,yy);
while(nivel[xx] > nivel[yy])
Min = max(Min,cost[xx]),xx = dad[xx];
while(xx != yy)
{
Min = max(Min , max(cost[xx],cost[yy]));
xx = dad[xx];
yy = dad[yy];
}
g << Min << '\n';
}
return 0;
}