Pagini recente » Cod sursa (job #2914573) | Cod sursa (job #459854) | Cod sursa (job #2426028) | Cod sursa (job #2612853) | Cod sursa (job #3283719)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("radiatie.in");
ofstream fout ("radiatie.out");
typedef pair<int,pair<int,int>> piii;
const int nmax=1e6+5;
vector <pair<int,int>> v[nmax];
vector <piii> g;
int n, m, k, t[nmax], h[nmax], dp[20][nmax], c[20][nmax];
void init (int n)
{
for (int i=1; i<=n; i++)
{
t[i]=i;
h[i]=1;
}
}
int getroot (int nod)
{
if (t[nod]!=nod)
t[nod]=getroot(t[nod]);
return t[nod];
}
void unif (int x, int y)
{
x=getroot(x);
y=getroot(y);
if (h[x]<h[y])
t[x]=y;
else if (h[x]>h[y])
t[y]=x;
else
{
t[y]=x;
h[x]++;
}
}
void kruskal ()
{
sort(g.begin(),g.end());
init(n);
for (auto i:g)
{
int x=getroot(i.second.first);
int y=getroot(i.second.second);
if (x!=y)
{
v[i.second.first].push_back({i.second.second,i.first});
v[i.second.second].push_back({i.second.first,i.first});
unif(x,y);
}
}
}
void dfs (int node, int x, int cost)
{
dp[0][node]=x;
c[0][node]=cost;
h[node]=h[x]+1;
for (auto i:v[node])
{
if (i.first!=x)
dfs(i.first,node,i.second);
}
}
int query (int x, int y)
{
int rez=0;
if (h[x]<h[y])
swap(x,y);
int k=h[x]-h[y];
for (int i=0; i<20; i++)
{
if ((1<<i)&k)
{
rez=max(rez,c[i][x]);
x=dp[i][x];
}
}
if (x==y)
return rez;
for (int i=19; i>=0; i--)
{
if (dp[i][x]!=dp[i][y])
{
rez=max(rez,c[i][x]);
rez=max(rez,c[i][y]);
x=dp[i][x];
y=dp[i][y];
}
}
return max(rez,max(dp[0][x],dp[0][y]));
}
int main()
{
fin >> n >> m >> k;
for (int i=1; i<=m; i++)
{
int x, y, c;
fin >> x >> y >> c;
g.push_back({c,{x,y}});
}
kruskal();
dfs(1,0,0);
for (int i=1; i<20; i++)
{
for (int j=1; j<=n; j++)
{
dp[i][j]=dp[i-1][dp[i-1][j]];
c[i][j]=max(c[i-1][j],c[i-1][dp[i-1][j]]);
}
}
for (int i=1; i<=k; i++)
{
int x, y;
fin >> x >> y;
fout << query(x,y) << " ";
}
return 0;
}