Pagini recente » Cod sursa (job #392365) | Istoria paginii runda/doerm/clasament | Cod sursa (job #1052585) | Istoria paginii runda/lejer/clasament | Cod sursa (job #2922205)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
int n, m, k;
int tata[15015], dim[15015];
int ancestor[15015][15], maxEdge[15015][15];
int nivel[15015];
struct elem
{
int x, y, cost;
bool operator < (const elem &other) const
{
return cost < other.cost;
}
};
vector<pair<int, int>> g[15015];
vector<elem> graph;
int tata_multime(int x)
{
if(x == tata[x])
{
return x;
}
return tata[x] = tata_multime(tata[x]);
}
void unire(int x, int y)
{
if(dim[x] < dim[y])
{
swap(x, y);
}
dim[x] += dim[y];
tata[y] = x;
}
void dfs(int fiu, int tata, int cost)
{
nivel[fiu] = 1 + nivel[tata];
if(tata != 0)
{
ancestor[fiu][0] = tata;
maxEdge[fiu][0] = cost;
for(int i = 1; (1 << i) <= nivel[fiu]; i ++)
{
ancestor[fiu][i] = ancestor[ancestor[fiu][i - 1]][i - 1];
maxEdge[fiu][i] = max(maxEdge[fiu][i - 1], maxEdge[ancestor[fiu][i - 1]][i - 1]);
}
}
for(auto it : g[fiu])
{
if(it.first == tata)
{
continue;
}
dfs(it.first, fiu, it.second);
}
}
int kthAncestor(int nod, int k)
{
int answer = nod;
for(int i = 0; (1 << i) <= k; i ++)
{
if((1 << i) & k)
{
answer = ancestor[answer][i];
}
}
return answer;
}
int valMax(int nod, int k)
{
int answer = 0;
for(int i = 0; (1 << i) <= k; i ++)
{
if((1 << i) & k)
{
answer = max(answer, maxEdge[nod][i]);
nod = ancestor[nod][i];
}
}
return answer;
}
int main()
{
fin >> n >> m >> k;
for(int i = 1; i <= n; i ++)
{
tata[i] = i;
dim[i] = 1;
}
while(m--)
{
int x, y, val;
fin >> x >> y >> val;
graph.push_back({x, y, val});
}
sort(graph.begin(), graph.end());
for(auto it : graph)
{
int x = tata_multime(it.x);
int y = tata_multime(it.y);
if(x != y)
{
unire(x, y);
g[it.x].push_back(make_pair(it.y, it.cost));
g[it.y].push_back(make_pair(it.x, it.cost));
}
}
dfs(1, 0, 0);
while(k--)
{
int x, y;
fin >> x >> y;
if(nivel[x] < nivel[y])
{
swap(x, y);
}
int answer = valMax(x, nivel[x] - nivel[y]);
x = kthAncestor(x, nivel[x] - nivel[y]);
if(x == y)
{
fout << answer << '\n';
continue;
}
int st = 1, dr = nivel[x];
int up = 0;
while(st <= dr)
{
int mid = (st + dr) >> 1;
if(kthAncestor(x, mid) == kthAncestor(y, mid))
{
up = mid;
dr = mid - 1;
}
else
{
st = mid + 1;
}
}
answer = max(answer, max(valMax(x, up), valMax(y, up)));
fout << answer << '\n';
}
return 0;
}