using System; using System.Collections.Generic; using System.Linq; public class Program { private class Source { private readonly int _id; private readonly uint _count; public Source(int id, uint count) { _id = id; _count = count; } public int Id { get { return _id; } } public uint Count { get { return _count; } } public Source Mul(uint count) { return new Source(_id, _count * count); } } private static List Dfs(List[] sourceList, List[] memo, int id, bool[] used) { if (sourceList[id].Count == 0) { return new List{ new Source(id, 1)}; } if (!used[id]) { var sources = new List(); foreach (var source in sourceList[id]) { var result = Dfs(sourceList, memo, source.Id, used); foreach (var element in result) { sources.Add(element.Mul(source.Count)); } } used[id] = true; memo[id] = sources; } return memo[id]; } public static void Main() { int n = int.Parse(Console.ReadLine()); int m = int.Parse(Console.ReadLine()); List[] list = new List[n + 1]; for (int i = 0; i < n + 1; i++) { list[i] = new List(); } for (int i = 0; i < m; i++) { var line = Console.ReadLine().Split(' ').Select(element => int.Parse(element)).ToArray(); list[line[2]].Add(new Source(line[0], (uint)line[1])); } bool[] used = new bool[n + 1]; var result = Dfs(list, new List[n+1], n, used); uint[] count = new uint[n+1]; foreach (var element in result) { count[element.Id] += element.Count; } for (int i = 1; i < n; i++) { Console.WriteLine(count[i]); } } }