Pagini recente » Cod sursa (job #3307020) | Monitorul de evaluare | Cod sursa (job #1269243) | Cod sursa (job #2765794) | Cod sursa (job #3311466)
#include <fstream>
#include <utility>
#define x first
#define y second
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("radiatie.in");
ofstream out("radiatie.out");
typedef pair <int, int> pii;
const int nmax = 15000, mmax = 30000, lgmax = 14;
int n, m, nrq, a, b, lg2n;
vector <pii> muchii[nmax + 2];
struct edge{
int a, b, costt;
bool operator < (const edge &obj) const {
return costt < obj.costt;
}
} edges[mmax + 2];
struct disjointset{
int father[nmax + 2];
int compsz[nmax + 2];
int getfather(int node){
if(node == father[node])
return node;
father[node] = getfather(father[node]);
return father[node];
}
void setmerge(int a, int b){
a = getfather(a);
b = getfather(b);
if(a == b){ return; }
if(compsz[a] < compsz[b])
swap(a, b);
compsz[a] += compsz[b];
father[b] = a;
}
} dsu;
int dpmaxtree[lgmax + 2][nmax + 2];
int fathertree[lgmax + 2][nmax + 2];
int depth[nmax + 2];
void dfs(int node, int father, int mxpath){
fathertree[0][node] = father;
dpmaxtree[0][node] = mxpath;
depth[node] = 1 + depth[father];
for(auto nxt : muchii[node]){
if(nxt.x != father)
dfs(nxt.x, node, nxt.y);
}
}
int query(int a, int b){
if(depth[a] > depth[b])
swap(a, b);
int mxx = 0, diff = depth[b] - depth[a];
for(int bit = lgmax; bit >= 0; bit--){
if(diff & (1 << bit)){
mxx = max(mxx, dpmaxtree[bit][b]);
b = fathertree[bit][b];
}
}
if(a == b){ return mxx; }
for(int bit = lgmax; bit >= 0; bit--){
if(fathertree[bit][a] != fathertree[bit][b]){
mxx = max(mxx, dpmaxtree[bit][a]);
mxx = max(mxx, dpmaxtree[bit][b]);
a = fathertree[bit][a];
b = fathertree[bit][b];
}
}
///a and b - child of lca(a, b)
mxx = max(mxx, dpmaxtree[0][a]);
mxx = max(mxx, dpmaxtree[0][b]);
return mxx;
}
int main(){
in>>n>>m>>nrq;
for(int i = 1; i <= m; i++)
in>>edges[i].a>>edges[i].b>>edges[i].costt;
for(int i = 1; i <= n; i++){
dsu.father[i] = i;
dsu.compsz[i] = 1;
}
sort(edges + 1, edges + 1 + m);
for(int i = 1; i <= m; i++){
if(dsu.getfather(edges[i].a) != dsu.getfather(edges[i].b)){
dsu.setmerge(edges[i].a, edges[i].b);
muchii[edges[i].a].push_back(make_pair(edges[i].b, edges[i].costt));
muchii[edges[i].b].push_back(make_pair(edges[i].a, edges[i].costt));
}
}
dfs(1, 0, 0);
for(int p = 1; p <= lgmax; p++){
for(int i = 1; i <= n; i++){
fathertree[p][i] = fathertree[p - 1][fathertree[p - 1][i]];
dpmaxtree[p][i] = max(
dpmaxtree[p - 1][i],
dpmaxtree[p - 1][fathertree[p - 1][i]]
);
}
}
for(int itq = 1; itq <= nrq; itq++){
in>>a>>b;
out<<query(a, b)<<"\n";;
}
return 0;
}