#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp" #include template struct Edge { int src, dst; Cost cost; Edge() = default; Edge(int src, int dst, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return cost < e.cost; } bool operator>(const Edge& e) const { return cost > e.cost; } }; template struct Graph : public std::vector>> { using std::vector>>::vector; void span(bool direct, int src, int dst, Cost cost = 1) { (*this)[src].emplace_back(src, dst, cost); if (!direct) (*this)[dst].emplace_back(dst, src, cost); } }; #line 2 "main.cpp" #include #line 4 "main.cpp" using namespace std; void solve() { int n, m; cin >> n >> m; Graph graph(n); while (m--) { int u, v, c; cin >> u >> v >> c; graph.span(false, --u, --v, c); } vector ans(n, -1); auto dfs = [&](auto&& f, int v) -> void { for (auto e : graph[v]) { int u = e.dst, c = e.cost; if (ans[u] != -1) { if ((ans[v] ^ ans[u]) != c) { cout << "-1\n"; exit(0); } } else { ans[u] = (ans[v] ^ c); f(f, u); } } }; for (int v = 0; v < n; ++v) { if (ans[v] != -1) continue; ans[v] = 0; dfs(dfs, v); } for (auto x : ans) cout << x << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }