Pagini recente » Cod sursa (job #477041) | Cod sursa (job #1780183) | Cod sursa (job #1133550) | oji_11_2023 | Cod sursa (job #487054)
Cod sursa(job #487054)
#include <cstdio>
#include <vector>
#include <algorithm>
const int maxK = 1010;
const int maxN = 250100;
using namespace std;
vector < pair <int, int> > T[maxK];
int N, M, x, y;
vector <int> G[maxN];
int P[maxN];
inline int tata(int x) {
if (x == P[x])
return x;
P[x] = tata(P[x]);
return P[x];
}
int main() {
int i, j, a, b, c;
freopen("pscnv.in", "r", stdin);
freopen("pscnv.out", "w", stdout);
scanf("%d%d%d%d", &N, &M, &x, &y);
for (i = 1; i <= M; i++) {
scanf("%d%d%d", &a, &b, &c);
T[c].push_back(make_pair(a, b));
}
for (i = 1; i <= N; i++)
P[i] = i;
for (i = 1; i <= 1000; i++) {
for (j = 0; j < T[i].size(); j++) {
if (tata(T[i][j].first) != tata(T[i][j].second))
P[tata(T[i][j].first)] = tata(T[i][j].second);
}
if (tata(x) == tata(y)) {
printf("%d\n", i);
return 0;
}
}
return 0;
}