Cod sursa(job #998428)

Utilizator smaraldaSmaranda Dinu smaralda Data 16 septembrie 2013 23:09:13
Problema Radiatie Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.05 kb
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<vector>
#define NMAX 30000
using namespace std;

int n,lvl[NMAX + 5],t[NMAX + 5],stramos[20][NMAX + 5],cost[20][NMAX + 5];
bool vis[NMAX + 5];

struct EDGE { int x,y,c; };
EDGE e[NMAX + 5];

struct EDGE2 { int v,c; };
vector <EDGE2> arb[NMAX + 5];

EDGE2 make (int a, int b) {
    EDGE2 ret = {a,b};
    return ret;
}

class cmp {
    public:
        bool operator () (EDGE a, EDGE b) {
            return a.c < b.c;
            }
    };

int tata (int nod) {
    if(t[nod] == nod)
        return nod;
    return tata(t[nod]);
}

void unite (int x, int y) {
    if(rand() & 1)
        t[x] = y;
    else
        t[y] = x;
}

void dfs (int nod, int T, int C) {
    int i;

    stramos[0][nod] = T;
    lvl[nod] = lvl[T] + 1;
    cost[0][nod] = C;
    vis[nod] = 1;

    for(i = 0; i < arb[nod].size(); i++)
        if(!vis[arb[nod][i].v])
            dfs(arb[nod][i].v,nod,arb[nod][i].c);
}

int urca (int nod, int l) {
    if(l == 0)
        return nod;
    int p;
    for(p = 0; (1<<p) <= l; p++);
    p--;
    return urca(stramos[p][nod],l - (1<<p));
}

int findCost (int nod, int l, int res) {
    if(l == 0)
        return res;
    int p;
    for(p = 0; (1<<p) <= l; p++);
    p--;
    return max(cost[p][nod],findCost(stramos[p][nod], l - (1<<p), res));
}

int LCA (int x, int y) {
    int p;
    if(x == y)
        return x;
    for(p = 0; stramos[p][x] != stramos[p][y]; p++);
    if(p)
        p--;
    return LCA(stramos[p][x],stramos[p][y]);
}

int main() {
    freopen("radiatie.in","r",stdin);
    freopen("radiatie.out","w",stdout);
    int q,x,y,resx,resy,lca,tx,ty,m,k,i,j;

    scanf("%d%d%d",&n,&m,&k);
    for(i = 1; i <= m; i++)
        scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].c);
    for(i = 1; i <= n; i++)
        t[i] = i;

    sort(e + 1, e + 1 + m, cmp());
    for(i = 1; i <= m; i++) {
        tx = tata(e[i].x);
        ty = tata(e[i].y);
        if(tx != ty) {
            unite(tx,ty);
            arb[e[i].x].push_back(make(e[i].y,e[i].c));
            arb[e[i].y].push_back(make(e[i].x,e[i].c));
            }
        }

    dfs(1,1,0);
    stramos[0][1] = cost[0][1] = 0;

    for(i = 1; (1<<i) <= n; i++)
        for(j = 1; j <= n; j++) {
            stramos[i][j] = stramos[i - 1][stramos[i - 1][j]];
            if(stramos[i][j])
                cost[i][j] = max(cost[i - 1][j], cost[i - 1][stramos[i - 1][j]]);
            }

    for(q = 1; q <= k; q++) {
        scanf("%d%d",&x,&y);
        resx = resy = 0;
        if(lvl[x] < lvl[y]) {
            resy = findCost(y,lvl[y] - lvl[x],0);
            y = urca(y,lvl[y] - lvl[x]);
            }
        if(lvl[y] < lvl[x]) {
            resx = findCost(x,lvl[x] - lvl[y],0);
            x = urca(x,lvl[x] - lvl[y]);
            }
        lca = LCA(x,y);
        resx = findCost(x,lvl[x] - lvl[lca],resx);
        resy = findCost(y,lvl[y] - lvl[lca],resy);
        printf("%d\n",max(resx,resy));
        }
    return 0;
}