Cod sursa(job #1392627)

Utilizator felixiPuscasu Felix felixi Data 18 martie 2015 19:42:05
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

ifstream in("pscnv.in");
ofstream out("pscnv.out");

const int NMAX = 250000;
const int MMAX = 500000;
const int KMAX = 1000;

void Next_i32( int &nr ) {
    nr = 0;
    char c;
    in.get(c);
    while( c < 48 || c > 58 ) in.get(c);
    while( c > 47 && c < 59 ) {
        nr = nr * 10 + c - 48;
        in.get(c);
    }
}

struct DAP {
    int x,y;
};

vector <DAP> L[KMAX+2];
int N, M, X, Y;
int tt[NMAX+2], gr[NMAX+2];

void Citire() {
    in >> N >> M >> X >> Y;
    for( int i = 1;  i <= N;  ++i ) { tt[i] = i; gr[i] = 1; }
    for( int i = 1;  i <= M;  ++i ) {
        int x,y,c;
        in >> x >> y >> c;
        DAP aux;  aux.x = x; aux.y = y;
        L[c].push_back( aux );
    }
}

int Root( int x ) {
    if( tt[x] == x ) return x;
    return ( tt[x] = Root( tt[x] ) );
}

void Union( int x, int y ) {
    int Tx = Root(x), Ty = Root(y);
    if( Tx != Ty ) {
        if( gr[Tx] < gr[Ty] ) {
            tt[Tx] = Ty;
            gr[Ty] += gr[Tx];
        }
        else {
            tt[Ty] = Tx;
            gr[Tx] += gr[Ty];
        }
    }
}

int main() {
    Citire();
    for( int i = 1;  i <= KMAX;  ++i ) {
        for( int j = 0;  j < (int)L[i].size();  ++j ) {
            Union( L[i][j].x, L[i][j].y );
        }
        if( Root(X) == Root(Y) ) {
            out << i << '\n';
            return 0;
        }
    }
    return 0;
}