Cod sursa(job #1586420)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 1 februarie 2016 09:47:58
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include<cstdio>
#include<algorithm>
#include<vector>
#define MAXN 15010
#define MAXM 30010
#define MAXLOG 20
#define inf 2000000000
using namespace std;
vector<pair<int,int> > g[MAXN];
struct edge{int a,b,c;};
edge edges[MAXM];
int dad[MAXN],h[MAXN],ancestor[MAXN][MAXLOG],maximum_distance[MAXN][MAXLOG],cost[MAXN],level[MAXN];
bool cmp(edge a,edge b){
    return a.c<b.c;
}
int find_dad(int node){
    while(node!=dad[node])
        node=dad[node];
    return node;
}
void dfs(int node){
    if(level[node]!=0)
        return;
    if(dad[node]==node){
        level[node]=1;
        return;
    }
    dfs(dad[node]);
    level[node]=level[dad[node]]+1;
}
int maxim(int a,int b){
    if(a<b)
        return b;
    return a;
}
int main(){
    freopen("radiatie.in","r",stdin);
    freopen("radiatie.out","w",stdout);
    int n,m,k,i,components,dad1,dad2,x,y,answer;
    scanf("%d%d%d",&n,&m,&k);
    for(i=1;i<=m;i++)
        scanf("%d%d%d",&edges[i].a,&edges[i].b,&edges[i].c);
    sort(edges+1,edges+m+1,cmp);
    for(i=1;i<=n;i++)
        dad[i]=i;
    components=n;
    for(i=1;i<=m&&components>1;i++){
        dad1=find_dad(edges[i].a);
        dad2=find_dad(edges[i].b);
        if(dad1==dad2)
            continue;
        components--;
        dad[dad2]=dad1;
        cost[dad2]=edges[i].c;
    }
    for(i=1;i<=n;i++)
        dfs(i);
    for(i=1;i<=k;i++){
        scanf("%d%d",&x,&y);
        answer=-inf;
        while(level[x]>level[y]){
            answer=maxim(answer,cost[x]);
            x=dad[x];
        }
        while(level[y]>level[x]){
            answer=maxim(answer,cost[y]);
            y=dad[y];
        }
        while(x!=y){
            answer=maxim(answer,maxim(cost[x],cost[y]));
            x=dad[x];
            y=dad[y];
        }
        printf("%d\n",answer);
    }
    return 0;
}