Cod sursa(job #1740414)

Utilizator akaprosAna Kapros akapros Data 11 august 2016 16:12:48
Problema PScNv Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>
#define maxN 250002
#define maxM 2 * maxN
using namespace std;
int n, m, p[maxN], x, y, ans;
struct edge
{
    int x, y, c;
}v[maxM];
int cmp(const edge a, const edge b)
{
    return a.c > b.c;
}
int root(int x)
{
    if (p[x] == 0)
        return x;
    return p[x] = root(p[x]);
}
void Union(int x, int y)
{
    int rx = root(x), ry = root(y);
    p[rx] = ry;
}
void read()
{
    int i;
    freopen("pscnv.in", "r", stdin);
    scanf("%d %d %d %d", &n, &m, &x, &y);
    for (i = 1; i <= m; ++ i)
        scanf("%d %d %d", &v[i].x, &v[i].y, &v[i].c);
}
void solve()
{
    int i = 0;
    sort(v + 1, v + m + 1, cmp);
    for (i = 1; root(x) != root(y); ++ i)
    {
        ans = v[i].c;
        Union(v[i].x, v[i].y);
    }
}
void write()
{
    freopen("pscnv.out", "w", stdout);
    printf("%d\n", ans);
}
int main()
{
    read();
    solve();
    write();
    return 0;
}