#include <bits/stdc++.h>   using namespace std;   struct EDGE {     EDGE(int _v, int _cap, int _edgeidx) : v(_v), cap(_cap), edgeidx(_edgeidx) {}     int v;     int cap;     int edgeidx;   };   int DEBUG = 0;   const int MAXN = 200001;   vector<EDGE> edge[MAXN];   int in[MAXN], total[MAXN], dir[MAXN];   int mark[MAXN];   int main(int argc, char **argv) {     DEBUG = (argc >= 2) ? atoi(argv[1]) : 0;     int n, m;     cin >> n >> m;     memset(total, 0, sizeof(total));     memset(in, 0, sizeof(in));     memset(mark, 0, sizeof(mark));     for (int i = 1; i <= m; i++) {       int a, b, c;       cin >> a >> b >> c;       edge[a].push_back(EDGE(b, c, i));       edge[b].push_back(EDGE(a, c, -i));       total[a] += c;       total[b] += c;     }     if (DEBUG) {       for (int i = 1; i <= m; i++) {         printf( total[%d]= %d  , i, total[i]);       }       printf(  n );     }     queue<int> que;     mark[1] = 1;     que.push(1);     while (!que.empty()) {       int u = que.front();       que.pop();       mark[u] = 1;       if (DEBUG) {         printf(  nu= %d n , u);       }       for (int i = 0; i < edge[u].size(); i++) {         int edgeidx = edge[u][i].edgeidx;         int v = edge[u][i].v;         int cap = edge[u][i].cap;         if (DEBUG) {           printf( u= %d, v= %d, edgeidx= %d, cap= %d, mark[%d]= %d n , u, v,                  edgeidx, cap, v, mark[v]);         }         if (mark[v] == 0) {           in[v] += cap;           if (total[v] == 2 * in[v] && v != n) {             if (DEBUG) {               printf( que.push(%d) n , v);             }             que.push(v);           }           dir[abs(edgeidx)] = ((edgeidx < 0) ? 1 : 0);         }       }       if (DEBUG) {         printf( que.size()= %d n , que.size());       }     }     for (int i = 1; i <= m; i++) {       cout << dir[i] << endl;     }     return 0;   }   