#include <bits/stdc++.h>   using namespace std;   enum { DUNNO = -1, AB, BA };   vector<pair<int, int> > E[200010];   map<pair<int, int>, int> edge_id;   int dir[200010];   int val[200010];   bool vis[200010];   int main() {     int N, M;     cin >> N >> M;     memset(val, 0, sizeof(val));     memset(vis, 0, sizeof(vis));     memset(dir, DUNNO, sizeof(dir));     int a, b, c;     for (int j = (1); j <= (M); ++j) {       scanf( %d%d%d , &a, &b, &c);       E[a].push_back(make_pair(b, c));       E[b].push_back(make_pair(a, c));       edge_id[make_pair(a, b)] = j;       edge_id[make_pair(b, a)] = -j;       val[a] += c;       val[b] += c;     }     for (int i = (2); i <= (N - 1); ++i) val[i] /= 2;     queue<int> Q;     val[1] = 0;     Q.push(1);     while (!Q.empty()) {       int i = Q.front();       Q.pop();       if (vis[i]) continue;       vis[i] = true;       assert(val[i] == 0);       int numE = E[i].size();       for (int x = 0; x < (numE); ++x) {         int j = E[i][x].first;         int c = E[i][x].second;         if (vis[j]) continue;         int e = edge_id[make_pair(i, j)];         if (dir[abs(e)] != DUNNO) continue;         if (e > 0)           dir[e] = 0;         else           dir[-e] = 1;         val[j] -= c;         if (!val[j]) Q.push(j);       }     }     for (int j = (1); j <= (M); ++j) cout << dir[j] << endl;   }   