#define PROBLEM "https://yukicoder.me/problems/no/1420"
#define ERROR 1 // Check only whether the answer is -1 or not (by hitonanode)
#include<bits/stdc++.h>#include"library/algebra/group/Xor.hpp"
#include"library/datastructure/unionfind/PotentialUnionFind.hpp"intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn,m;std::cin>>n>>m;PotentialUnionFind<GroupXor<int>>UF(n);while(m--){inta,b,y;std::cin>>a>>b>>y;a--;b--;if(!UF.merge(a,b,y)){std::cout<<-1<<"\n";return0;}}for(inti=0;i<n;i++)std::cout<<UF.from_root(i).second<<"\n";}
#line 1 "test/yukicoder/1420.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1420"
#define ERROR 1 // Check only whether the answer is -1 or not (by hitonanode)
#include<bits/stdc++.h>#line 1 "library/algebra/group/Xor.hpp"
template<typenameX>structGroupXor{usingvalue_type=X;staticconstexprXop(constX&x,constX&y)noexcept{returnx^y;}staticconstexprvoidRchop(X&x,constX&y){x^=y;}staticconstexprvoidLchop(constX&x,X&y){y^=x;}staticconstexprXinverse(constX&x)noexcept{returnx;}staticconstexprXpower(constX&x,longlongn)noexcept{return(n&1?x:0);}staticconstexprXunit(){returnX(0);}staticconstexprboolcommute=true;};#line 2 "library/datastructure/unionfind/PotentialUnionFind.hpp"
template<typenameAbelGroup>classPotentialUnionFind{usingT=typenameAbelGroup::value_type;intn,num;std::vector<int>sz,parent;std::vector<T>potential;// parent[x] を基準とした時の x の値public:PotentialUnionFind()=default;PotentialUnionFind(intn):n(n),num(n),sz(n,1),parent(n,0),potential(n,AbelGroup::unit()){assert(AbelGroup::commute);std::iota(parent.begin(),parent.end(),0);}std::pair<int,T>from_root(intx){if(x==parent[x])return{x,AbelGroup::unit()};auto[r,add]=from_root(parent[x]);parent[x]=r;AbelGroup::Rchop(potential[x],add);return{r,potential[x]};}intleader(intx){returnfrom_root(x).first;}boolsame(intx,inty){assert(0<=xandx<nand0<=yandy<n);returnleader(x)==leader(y);}boolmerge(intx,inty,Td){// potential[y]-potential[x]=d にする// 矛盾する場合は変更はせず false を返すassert(0<=xandx<nand0<=yandy<n);auto[rx,dx]=from_root(x);auto[ry,dy]=from_root(y);AbelGroup::Rchop(d,dx);AbelGroup::Rchop(d,AbelGroup::inverse(dy));if(rx==ry)returnd==AbelGroup::unit();if(sz[rx]<sz[ry]){std::swap(rx,ry);d=AbelGroup::inverse(d);}sz[rx]+=sz[ry];parent[ry]=rx;potential[ry]=d;num--;returntrue;}std::optional<T>diff(intx,inty){// x を基準とするauto[rx,dx]=from_root(x);auto[ry,dy]=from_root(y);if(rx!=ry)returnstd::nullopt;returnAbelGroup::op(dy,AbelGroup::inverse(dx));}intsize(constintx){assert(0<=xandx<n);returnsz[leader(x)];}intcount()const{returnnum;}};#line 7 "test/yukicoder/1420.test.cpp"
intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);intn,m;std::cin>>n>>m;PotentialUnionFind<GroupXor<int>>UF(n);while(m--){inta,b,y;std::cin>>a>>b>>y;a--;b--;if(!UF.merge(a,b,y)){std::cout<<-1<<"\n";return0;}}for(inti=0;i<n;i++)std::cout<<UF.from_root(i).second<<"\n";}