Cod sursa(job #2698057)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 20 ianuarie 2021 19:27:49
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.9 kb
#include <bits/stdc++.h>

using namespace std;

class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};

InParser fin("pscnv.in");
ofstream fout("pscnv.out");

class edge {
    public:
        int u, v, w;

        bool operator < (const edge &A) const {
            return w < A.w;
        }
};

class DSU {
    public:
        int N;
        vector<int> p;

        DSU(int n) : N(n) {
            p.resize(N + 1);
            iota(p.begin(), p.end(), 0);
        }

        int get(int x) {
            if(p[x] == x)
                return x;
            return p[x] = get(p[x]);
        }

        void unite(int u, int v) {
            p[u] = v;
        }
};

const int NMAX = 2e5 + 5e4 + 1;
int N, M, x, y;
edge edges[NMAX];

int main() {
    fin >> N >> M >> x >> y;
    for(int i = 1; i <= M; ++i)
        fin >> edges[i].u >> edges[i].v >> edges[i].w;
    sort(edges + 1, edges + M + 1);
    DSU tree(N);
    for(int i = 1; i <= M; ++i) {
        if(tree.get(edges[i].u) != tree.get(edges[i].v))
            tree.unite(edges[i].v, edges[i].u);
        if(tree.get(x) == tree.get(y)) {
            fout << edges[i].w;
            return 0;
        }
    }
}