Cod sursa(job #1457058)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 2 iulie 2015 16:53:25
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <cstdio>
#include <fstream>
#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];

ifstream in("pscnv.in");

#define MaxChar 1000
#define verf ( (++CharB==MaxChar) ? ( in.read(Buffer,MaxChar),CharB=0 ) : (1) )

long CharB = MaxChar - 1;
char Buffer[MaxChar];

void cit(int &a)
{
    bool ok = 0;
    for( ; !( (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9') || ( Buffer[ CharB ] == '-' ) ); verf )
        ;
    if ( Buffer[ CharB ] == '-' )
    {
        CharB++;
        ok=1;
    }
    for( a=0; (Buffer[ CharB ]>='0' && Buffer[ CharB ]<='9'); a*=10,a+=( Buffer[ CharB ]-'0'), verf )
        ;
    if(ok)
        a=-a;
}

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.out", "w", stdout);
    cit(n); cit(m); cit(X); cit(Y);
    for(int i = 1; i <= m; ++i){
        int a = 0, b = 0, c = 0;
        cit(a); cit(b); cit(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;
}