#include #include #include #include #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) using namespace std; using namespace atcoder; using namespace __gnu_pbds; using ll=long long; using ld=long double; using vll=vector; using vvll=vector; using pll=pair; // using mint=modint; // template // using ordered_map=tree,rb_tree_tag,tree_order_statistics_node_update>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N,Q; cin>>N>>Q; vector>g(N); rep(i,0,Q){ ll A,B,C; cin>>A>>B>>C; --A,--B; g[A].push_back({B,C}); g[B].push_back({A,C}); } vll col(N,-1); ll comp=0; rep(s,0,N){ if(col[s]!=-1)continue; comp++; col[s]=0; queueq; q.push(s); while(!q.empty()){ ll u=q.front(); q.pop(); for(auto [v,c]:g[u]){ ll nc=col[u]^c; if(col[v]==-1){ col[v]=nc; q.push(v); }else if(col[v]!=nc){ cout<<0<