#include #include #include #include std::vector topological_sort(int N, std::vector> edges) { std::vector ins(N); std::vector> outs(N); for (const auto &[v1, v2]: edges) { ins.at(v2)++; outs.at(v1).push_back(v2); } std::queue q; for (int v2 = 0; v2 < N; v2++) { if (ins.at(v2) == 0) { q.push(v2); } } std::vector res; while (!q.empty()) { const auto v1 = q.front(); q.pop(); res.push_back(v1); for (const auto &v2: outs.at(v1)) { ins.at(v2)--; if (ins.at(v2) == 0) { q.push(v2); } } } return res; } int main() { int N, M; std::cin >> N; std::cin >> M; std::vector> edges; std::vector>> materials(N); for (int i = 0; i < M; i++) { int P, Q, R; std::cin >> P >> Q >> R; P--; R--; edges.emplace_back(P, R); materials.at(R).emplace_back(P, Q); } auto tps = topological_sort(N, edges); std::reverse(tps.begin(), tps.end()); std::vector ans(N, 0); ans.at(N - 1) = 1; for (const auto &x: tps) { for (const auto &[p, q]: materials.at(x)) { ans.at(p) += q * ans.at(x); } if (!materials.at(x).empty()) { ans.at(x) = 0; } } for (int i = 0; i < N - 1; i++) { std::cout << ans.at(i) << std::endl; } }