Pagini recente » Cod sursa (job #2083168) | Cod sursa (job #2928543) | Cod sursa (job #324738) | Cod sursa (job #1326189) | Cod sursa (job #2298833)
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
ifstream fi("radiatie.in");
ofstream fo("radiatie.out");
typedef struct much{int x,y,c;} MUCH;
MUCH A[30005];
int n,m,k,i,j,x,y,P[15005][15],C[15005][15],Pa[15005],Sz[15005],Lg[15005],Niv[15005],rez;
vector<pair<int,int> > V[15005];
bool cmp(MUCH a, MUCH b)
{
return a.c<b.c;
}
int p(int nod)
{
int cnod=nod;
while(Pa[nod])
nod=Pa[nod];
int aux;
while(Pa[cnod])
{
aux=Pa[cnod];
Pa[cnod]=nod;
cnod=aux;
}
return nod;
}
void u(int x, int y)
{
if(Sz[x]>=Sz[y])
{
Pa[y]=x;
Sz[x]+=Sz[y];
}
else
{
Pa[x]=y;
Sz[y]+=Sz[x];
}
}
void dfs(int nod, int p, int cost)
{
vector<pair<int,int> >::iterator it;
P[nod][0]=p;
Niv[nod]=1+Niv[p];
C[nod][0]=cost;
int i;
for(i=1; i<=Lg[n]; i++)
{
P[nod][i]=P[P[nod][i-1]][i-1];
C[nod][i]=max(C[nod][i-1],C[P[nod][i-1]][i-1]);
}
for(it=V[nod].begin(); it!=V[nod].end(); it++)
if((*it).first!=p)
dfs((*it).first,nod,(*it).second);
}
int main()
{
fi>>n>>m>>k;
for(i=1; i<=m; i++)
fi>>A[i].x>>A[i].y>>A[i].c;
sort(A+1,A+m+1,cmp);
for(i=1; i<=n; i++)
Sz[i]=1;
for(i=1; i<=m; i++)
{
if(p(A[i].x)!=p(A[i].y))
{
u(p(A[i].x),p(A[i].y));
V[A[i].x].push_back({A[i].y,A[i].c});
V[A[i].y].push_back({A[i].x,A[i].c});
}
}
for(i=2; i<=n; i++)
Lg[i]=1+Lg[i/2];
dfs(1,0,0);
for(i=1; i<=k; i++)
{
fi>>x>>y;
if(Niv[x]>Niv[y])
swap(x,y);
j=Lg[n];
rez=0;
while(Niv[y]!=Niv[x])
{
if(Niv[P[y][j]]>=Niv[x])
{
rez=max(rez,C[y][j]);
y=P[y][j];
}
j--;
}
j=Lg[n];
while(P[x][0]!=P[y][0])
{
if(P[x][j]!=P[y][j])
{
rez=max(rez,C[x][j]);
rez=max(rez,C[y][j]);
x=P[x][j];
y=P[y][j];
}
j--;
}
if(x!=y)
{
rez=max(rez,C[x][0]);
rez=max(rez,C[y][0]);
}
fo<<rez<<"\n";
}
fi.close();
fo.close();
return 0;
}