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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <iostream> #include <vector> #include <unordered_map> #include <tuple> #include <cstdio>
#define int long long #define function auto #define rop(a, b, c) for(int a = b; a < c; ++ a) #define por(a, b, c) for(int a = b; a > c; -- a) #define rep(a, b, c) for(int a = b; a <= c; ++ a) #define per(a, b, c) for(int a = b; a >= c; -- a)
namespace Space {
template < typename _Tp > inline function read(_Tp& t) -> void { int f = 0, ch = getchar (); t = 0; while (!isdigit (ch)) f |= (ch == '-'), ch = getchar (); do {t = (t << 1) + (t << 3) + (ch & 15); ch = getchar ();} while (isdigit(ch)); t = f ? -t : t; }
template < typename _Tp, typename... _Args > inline function read(_Tp& t, _Args&... args) -> void {read(t); read(args...);} }
using namespace Space;
class Find { public: std::vector<int> parent, rank, xorValue; Find(int n) : parent(n), rank(n, 0), xorValue(n, 0) { rop(i, 0, n) parent[i] = i; } public: inline function find(int x) -> int { if (parent[x] != x) { int root = this -> find(parent[x]); xorValue[x] ^= xorValue[parent[x]]; parent[x] = root; } return parent[x]; }
inline function uunion(int x, int y, int value) -> bool { int rootX = this -> find(x); int rootY = this -> find(y); if (rootX == rootY) return (xorValue[x] ^ xorValue[y]) == value; if (rank[rootX] < rank[rootY])std::swap(rootX, rootY); parent[rootY] = rootX; xorValue[rootY] = xorValue[x] ^ xorValue[y] ^ value; if (rank[rootX] == rank[rootY])rank[rootX] ++; return true; } };
signed main() { int n, m;read(n, m); std::vector<int> u(m), v(m), f(m); rop(i, 0, m){ read(u[i], v[i], f[i]); -- u[i]; -- v[i]; } Find uf(n); bool possible = true; rop(i, 0, m) { if (!uf.uunion(u[i], v[i], f[i])) { possible = false; break; } } if (!possible) std::printf("No\n"); else { std::printf("Yes\n"); std::vector<int> a(m, 0); rop(i, 0, m) a[i] = f[i]; for (auto x : a) std::cout << x << ' '; std::printf("\n"); } return 0; }
|