Cod sursa(job #1083605)

Utilizator paunmatei7FMI Paun Matei paunmatei7 Data 16 ianuarie 2014 09:17:55
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <fstream>

#define x first
#define y second
#define NMAX 250007
#define MaxChar 100000
#define verf ( (++CharB==MaxChar) ? ( in.read(Buffer,MaxChar),CharB=0 ) : (1) )

using namespace std;

ifstream in("pscnv.in");

int H[NMAX], T[NMAX];
int n, m, X, Y;
vector< pair< int, int > > v[1007];
int 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 <= m; ++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;
        }
    }
    return 0;
}