Pagini recente » Cod sursa (job #2367179) | Cod sursa (job #1703719) | Cod sursa (job #1169088) | Cod sursa (job #57034) | Cod sursa (job #2763148)
#include <fstream>
#include <vector>
#include <algorithm>
#define NMAX 15005
#define PMAX 17
using namespace std;
ifstream cin("radiatie.in");
ofstream cout("radiatie.out");
struct muchie
{
int x,y,c;
friend bool operator < (const muchie& a, const muchie& b)
{
return a.c<b.c;
}
}aux;
int n,m,k;
int sef[NMAX],ad[NMAX];
pair <int,int> t[NMAX][PMAX];
vector <muchie> e;
vector <muchie> arb;
vector < pair <int, int> > adj[NMAX];
int sef_sup(int nod)
{
if(sef[nod]!=nod)
return sef[nod]=sef_sup(sef[nod]);
else
return nod;
}
void unire(int nod1, int nod2)
{
sef[sef_sup(nod1)]=sef_sup(nod2);
}
void apm()
{
sort(e.begin(),e.end());
for(int i=1;i<=n;i++)
sef[i]=i;
for(int i=0;i<e.size();i++)
{
aux=e[i];
if(sef_sup(aux.x)!=sef_sup(aux.y))
{
unire(aux.x,aux.y);
arb.push_back(aux);
}
}
}
void dfs(int nod, int tata)
{
ad[nod]=ad[tata]+1;
for(pair <int,int> v : adj[nod])
if(v.second!=tata)
{
dfs(v.second,nod);
t[v.second][0]={v.first,nod};
}
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
cin>>aux.x>>aux.y>>aux.c;
e.push_back(aux);
}
apm();
for(int i=0;i<arb.size();i++)
{
aux=arb[i];
adj[aux.x].push_back({aux.c,aux.y});
adj[aux.y].push_back({aux.c,aux.x});
}
dfs(1,0);
bool ok=true;
for(int p=1;ok && ((1 << p) < n);p++)
{
ok=false;
for(int i=1;i<=n;i++)
{
t[i][p]={max(t[i][p-1].first,t[t[i][p-1].second][p-1].first),t[t[i][p-1].second][p-1].second};
if(t[i][p].second)
ok=true;
}
}
for(int i=1;i<=k;i++)
{
int x,y,rasp=-1;
cin>>x>>y;
if(ad[x]>ad[y])
swap(x,y);
for(int p=15;p>=0;p--)
if( (1 << p) <=ad[y]-ad[x])
{
rasp=max(rasp,t[y][p].first);
y=t[y][p].second;
}
for(int p=15;p>=0 && x!=y;p--)
if(t[x][p].second!=t[y][p].second)
{
rasp=max(rasp,t[x][p].first);
rasp=max(rasp,t[y][p].first);
x=t[x][p].second;
y=t[y][p].second;
}
if(x!=y)
{
rasp=max(rasp,t[x][0].first);
rasp=max(rasp,t[y][0].first);
}
cout<<rasp<<'\n';
}
return 0;
}