#include typedef long long ll; typedef unsigned long long ull; #define FOR(i,a,b) for(int (i)=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define RANGE(vec) (vec).begin(),(vec).end() using namespace std; class TakoyakiFactory { public: struct Edge { Edge(int t, ll c) : to(t), cost(c) {} int to; ll cost; }; void solve(void) { int N,M; cin>>N>>M; // 木を逆にたどればよい // dfs + 遅延評価 vector> tree(N); vector ins(N,0); // 入ってくる辺の数 REP(i,M) { int p,q,r; cin>>p>>q>>r; --p; --r; tree[r].emplace_back(p,q); ++ins[p]; } // N 以外のノードで入ってくる辺の数が 0 のものは取り除く REP(i, N-1) { if (ins[i] > 0) continue; for (auto e : tree[i]) --ins[e.to]; } vector sum(N,0); vector cache(N,0); vector vis(N,0); function dfs = [&](int x, ll n) { ++vis[x]; cache[x] += n; if (vis[x] < ins[x]) return; // 入ってくる辺がたまったら次の辺を見る if (tree[x].empty()) { sum[x] = cache[x]; return; } for (auto e : tree[x]) dfs(e.to, cache[x]*e.cost); }; dfs(N-1,1); REP(i,N-1) cout<solve(); delete obj; return 0; } #endif