using System; using System.Collections.Generic; using System.IO; using System.Linq; public class Program { public static void Main() { Reader reader = new Reader(Console.In); int N = reader.RInt(); int M = reader.RInt(); var zairyo = new List(); for (int i = 0; i < M; i++) { zairyo.Add(new Zairyo { moto = reader.RInt(), qty = reader.Int(), saki = reader.Int(), }); } var ans = new List(); for (int i = 1; i <= N; i++) { ans.Add(new Ans { product = i, canBuy = !zairyo.Any(x => x.saki == i) }); } ans.FirstOrDefault(x => x.product == N).qty = 1; while (ans.Any(x => !x.canBuy && x.qty != 0)) { foreach (var item in ans.Where(x => !x.canBuy && x.qty != 0)) { foreach (var it in zairyo.Where(x => x.saki == item.product)) { ans.FirstOrDefault(x => x.product == it.moto).qty += it.qty * item.qty; } item.qty = 0; } } foreach(var item in ans.Where(x => x.product != N)) { Console.WriteLine(item.qty); } } } public class Ans { public int product; public bool canBuy; public long qty; } public class Zairyo { public int moto; public int qty; public int saki; } public class Reader { private readonly TextReader reader; private readonly char separator = ' '; private string[] A = new string[0]; private int i; public Reader(TextReader r) { reader = r; } public string String() { return Set(); } public int Int() { return int.Parse(Set()); } public long Long() { return long.Parse(Set()); } public string RString() { return RSet(); } public int RInt() { return int.Parse(RSet()); } public long RLong() { return long.Parse(RSet()); } public int[] IntArray() { return A.Select(x => int.Parse(x)).ToArray(); } private void Read() { string line = reader.ReadLine(); A = line.Split(separator); i = 0; } private string Set() { return A[i++]; } private string RSet() { Read(); return A[i++]; } }