Cod sursa(job #2677777)

Utilizator FrostfireMagirescu Tudor Frostfire Data 27 noiembrie 2020 14:39:05
Problema PScNv Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.15 kb
#include <cstdio>
#include <algorithm>
#include <ctype.h>
#include <vector>
#include <queue>
#define NMAX 250000

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& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long 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;
	}
};

int n, m, x, y, tata[NMAX+10], rang[NMAX+10], val1, val2;
struct date
{   int n1, n2, c;
}K[NMAX+10];

int findDaddy(int x)
{   int r = x, y = x;
    while(tata[r] != r) r = tata[r];
    while(tata[x] != x)
        {   y = tata[x];
            tata[x] = r;
            x = y;
        }
    return r;
}

void unite(int x, int y)
{   if(rang[x] < rang[y]) tata[x] = y;
    else tata[y] = x;

    if(rang[x] == rang[y]) rang[x]++;
}

bool mycmp(date a, date b)
{   if(a.c < b.c) return 1;
    return 0;
}

int main()
{
    InParser f("pscnv.in");
    freopen("pscnv.out", "w", stdout);
    f >> n >> m >> x >> y;
    for(int i=1; i<=n; i++) tata[i] = i, rang[i] = 1;
    for(int i=1; i<=m; i++) f >> K[i].n1 >> K[i].n2 >> K[i].c;
    sort(K+1, K+m+1, mycmp);
    for(int i=1; i<=m; i++)
        {   val1 = findDaddy(K[i].n1);
            val2 = findDaddy(K[i].n2);
            if(val1 == val2) continue;
            unite(val1, val2);
            if(findDaddy(x) == findDaddy(y))
                {   printf("%d\n", K[i].c);
                    return 0;
                }
        }
    return 0;
}