#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 20;
const int maxm = 50000 + 20;
struct Arc {
int from, to, val;
Arc() {}
Arc(int from, int to, int val):from(from), to(to), val(val) {}
};
vector<Arc> arc;
int n, m;
bool dead[maxn];
vector<int> G[maxn];
/*{ SCC*/
int pre[maxn], lowlink[maxn], sccno[maxn], vex_cnt[maxn], dfs_clock, scc_cnt;
stack<int> S;
void dfs(int u) {
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(size_t i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
} else if(!sccno[v]) {
lowlink[u] = min(lowlink[u], pre[v]);
}
}
if(lowlink[u] == pre[u]) {
scc_cnt++;
int x;
do {
x = S.top(); S.pop();
sccno[x] = scc_cnt;
vex_cnt[scc_cnt]++;
} while(u != x);
}
}
void find_scc() {
dfs_clock = scc_cnt = 0;
memset(sccno, 0, sizeof sccno);
memset(pre, 0, sizeof pre);
memset(vex_cnt, 0, sizeof vex_cnt);
for(int i = 1; i <= n; ++i) {
if(!pre[i]) dfs(i);
}
}
/*}*/
void kill(int u) {
dead[u] = 1;
for(size_t i = 0; i < G[u].size(); ++i) {
if(!dead[G[u][i]]) {
kill(G[u][i]);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
arc.push_back(Arc(a, b, c));
if(c == 0) {
if(a == b) {
dead[a] = true;
} else {
G[a].push_back(b);
}
}
}
find_scc();
for(int i = 1; i <= n; ++i) {
G[i].clear();
}
for(size_t i = 0; i < arc.size(); ++i) {
G[arc[i].from].push_back(arc[i].to);
}
for(int i = 1; i <= n; ++i) {
if(1 < vex_cnt[sccno[i]] || dead[i]) {
kill(i);
}
}
for(int i = 1; i <= n; ++i) {
printf("%d\n", !dead[i]);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnV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