Pagini recente » Cod sursa (job #304574) | Cod sursa (job #2869945) | Cod sursa (job #2024556) | Cod sursa (job #2476321) | Cod sursa (job #2926814)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("radiatie.in");
ofstream fout("radiatie.out");
const int dim=15009,LOG=18;
class DSU{
int parent[dim],sz[dim];
public:
DSU(int n){
for(int i=1;i<=n;i++){
parent[i]=i;
sz[i]=1;
}
}
int find_parent(int x){
if(x==parent[x]){
return x;
}
return parent[x]=find_parent(parent[x]);
}
void union_sets(int x,int y){
x=find_parent(x);
y=find_parent(y);
if(x!=y){
if(sz[x]<sz[y]){
swap(x,y);
}
parent[y]=x;
sz[x]+=sz[y];
}
}
};
struct edge{
int x,y,cost;
bool operator <(const edge &other) const{
return cost<other.cost;
}
};
vector<pair<int,int> >adj[dim];
vector<edge>e;
int depth[dim];
int up[dim][20];
int rmq[dim][20];
void dfs(int x,int parent,int cost_parent_edge){
depth[x]=depth[parent]+1;
up[x][0]=parent;
rmq[x][0]=cost_parent_edge;
for(int i=1;i<=LOG;i++){
up[x][i]=up[up[x][i-1]][i-1];
rmq[x][i]=max(rmq[x][i-1],rmq[up[x][i-1]][i-1]);
}
for(int i=0;i<adj[x].size();i++){
pair<int,int> it=adj[x][i];
if(it.first!=parent){
dfs(it.first,x,it.second);
}
}
}
int maxEdge(int x,int y){
if(depth[x]<depth[y]){
swap(x,y);
}
int maxim=0;
int dif=depth[x]-depth[y];
for(int i=LOG;i>=0;i--){
if(dif&(1<<i)){
maxim=max(maxim,rmq[x][i]);
x=up[x][i];
}
}
for(int i=LOG;i>=0;i--){
if(up[x][i]!=up[y][i]){
maxim=max(maxim,max(rmq[x][i],rmq[y][i]));
x=up[x][i];
y=up[y][i];
}
}
if(x!=y){
maxim=max(maxim,max(rmq[x][0],rmq[x][1]));
}
return maxim;
}
signed main(){
int n,m,k;
fin>>n>>m>>k;
for(int i=1;i<=m;i++){
int x,y,cost;
fin>>x>>y>>cost;
e.push_back({x,y,cost});
}
DSU apm(n);
sort(e.begin(),e.end());
for(int i=0;i<e.size();i++){
int x=e[i].x,y=e[i].y,cost=e[i].cost;
if(apm.find_parent(x)!=apm.find_parent(y)){
apm.union_sets(x,y);
adj[x].push_back({y,cost});
adj[y].push_back({x,cost});
}
}
dfs(1,0,0);
while(k--){
int x,y;
fin>>x>>y;
fout<<maxEdge(x,y)<<'\n';
}
}