Pagini recente » Cod sursa (job #536943) | Cod sursa (job #1771545) | Cod sursa (job #688039) | Cod sursa (job #3273338) | Cod sursa (job #3291320)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("radiatie.in");
ofstream fout ("radiatie.out");
vector <pair<int, pair<int, int>>> v;
int root[15005];
int height[150005];
int tata[20][15005];
int medg[20][15005];
int lg2[15005];
vector <int> g[15005];
int find_root (int x)
{
if (root[x]!=x) {
int r = find_root(root[x]);
root[x] = r;
return r;
}
return x;
}
bool viz[15005];
int depth[15005];
void find_depth (int nod, int d)
{
viz[nod] = 1;
depth[nod] = d;
for (int vecin : g[nod]) {
if (!viz[vecin]) {
find_depth(vecin, d+1);
}
}
}
int main()
{
fin.tie(0); fin.sync_with_stdio(false);
int n, m, k; fin>>n>>m>>k;
for (int i=1; i<=m; i++) {
int x, y, c;
fin>>x>>y>>c;
v.push_back({c, {x, y}});
}
for (int i=1; i<=n; i++) root[i]=i;
sort(v.begin(), v.end());
for (int i=0; i<(int) v.size(); i++) {
int c = v[i].first;
int x = v[i].second.first;
int y = v[i].second.second;
int root_x = find_root(x);
int root_y = find_root(y);
if (root_x!=root_y) {
// In root_x tinem arborele mai mare
if (height[root_x]<height[root_y]) {
swap(root_x, root_y);
swap(x, y);
}
root[root_y] = root_x;
height[root_x]++;
if (tata[0][y]!=0) swap(x, y);
tata[0][y]=x;
medg[0][y]=c;
g[x].push_back(y);
g[y].push_back(x);
//cout<<x<<' '<<y<<' '<<c<<'\n';
}
}
int nod=1;
while (tata[0][nod]!=0) nod = tata[0][nod];
int main_root = nod;
find_depth(main_root, 0);
// cout<<main_root<<endl;
for (int i=2; i<=n; i++) lg2[i]=lg2[i/2]+1;
//cout<<endl<<endl;
//for (int j=1; j<=n; j++) {
// cout<<medg[0][j]<<' ';
//}
//cout<<endl;
for (int i=1; (1<<i)<=n; i++) {
for (int j=1; j<=n; j++) {
tata[i][j]=tata[i-1][tata[i-1][j]];
medg[i][j] = max(medg[i - 1][j], medg[i - 1][tata[i - 1][j]]);
//cout<<medg[i][j]<<' ';
}
// cout<<endl;
}
// cout<<endl<<endl;
while (k--) {
int x, y;
fin>>x>>y;
// int cx, cy; cx=x; cy=y;
int dx = depth[x], dy = depth[y];
if (dx<dy) {
swap(dx, dy);
swap(x, y);
}
int diff = dx-dy;
int maxi_muchie = -1;
while (diff) {
int strat = lg2[diff];
maxi_muchie = max(maxi_muchie, medg[strat][x]);
x = tata[strat][x];
dx = depth[x];
diff = dx-dy;
}
if (x==y) {
// cout<<cx<<' '<<cy<<' '<<x<<' '<<maxi_muchie<<endl;
fout<<maxi_muchie<<'\n';
continue;
}
for (int i=n; i>=0; i--) {
int rx = tata[i][x];
int ry = tata[i][y];
if (rx!=ry) {
int mx = medg[i][x];
int my = medg[i][y];
maxi_muchie = max(maxi_muchie, max(mx, my));
x = tata[i][x];
y = tata[i][y];
}
}
// int lca = tata[0][x];
maxi_muchie = max(maxi_muchie, max(medg[0][x], medg[0][y]));
// cout<<cx<<' '<<cy<<' '<<lca<<' '<<maxi_muchie<<endl;
fout<<maxi_muchie<<'\n';
}
return 0;
}