Cod sursa(job #1457055)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 2 iulie 2015 16:50:59
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define x first
#define y second
#define NMAX 250007

using namespace std;

int H[NMAX], T[NMAX];
int n, m, X, Y;
vector< pair< int, int > > v[1007];

void unite(int x, int y){
    if(H[x] == H[y]){
        ++ H[x];
        T[y] = x;
    }
    else
        if(H[x] < H[y])
            T[x] = y;
        else
            T[y] = x;
}

inline int father(int x){
    if(x == T[x])
        return x;
    return father(T[x]);
}

int main(){
    freopen("pscnv.in", "r", stdin);
    freopen("pscnv.out", "w", stdout);
    scanf("%d %d %d %d", &n, &m, &X, &Y);
    for(int i = 1; i <= m; ++i){
        int a = 0, b = 0, c = 0;
        scanf("%d %d %d", &a, &b, &c);
        v[c].push_back(make_pair(a, b));
        T[i] = i;
        H[i] = 1;
    }
    for(int i = 1; i <= 1000; ++i){
        for(vector< pair<int, int> >::iterator it = v[i].begin(); it != v[i].end(); ++it){
            int Tx = father(it->x), Ty = father(it->y);
            if(Tx != Ty)
                unite(Tx, Ty);
        }
        if(father(X) == father(Y)){
            printf("%d", i);
            return 0;
        }
    }
    printf("1001\n");
    return 0;
}