1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args<< " -> "; err(args); } while (false) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 1E4 + 100; struct E { int u, v; LL d; }; vector<E> edges; int n, m, fa[maxn], st, ed;
int findset(int x) { return fa[x] == -1 ? x : fa[x] = findset(fa[x]); } void join(int u, int v) { int fu = findset(u), fv = findset(v); if (fu != fv) fa[fu] = fv; }
int main() { memset(fa, -1, sizeof fa); cin >> n >> m; FOR (_, 0, m) { int u, v; LL d; scanf("%d%d%lld", &u, &v, &d); edges.push_back({u, v, d}); join(u, v); } cin >> st >> ed; if (findset(st) != findset(ed)) { puts("-1"); return 0; } LL ans = (1LL << 62) - 1; FORD (i, 61, -1) { ans ^= 1LL << i; memset(fa, -1, sizeof fa); for (E& e: edges) if ((e.d & ~ans) == 0) join(e.u, e.v); if (findset(st) != findset(ed)) ans ^= 1LL << i; } cout << ans << endl; }
|