using System; using System.Collections.Generic; using System.Text; class Program { public void Proc() { Reader.IsDebug = false; int lastSeihin = int.Parse(Reader.ReadLine()); int seizouhouCount = int.Parse(Reader.ReadLine()); for (int i = 1; i <= lastSeihin; i++) { Seihin tmp = new Seihin(); tmp.SeihinId = i; this.SeihinList.Add(tmp); } for (int i = 0; i < seizouhouCount; i++) { int[] inpt = Reader.GetInt(); int makeFor = inpt[2]; int cnt = inpt[1]; int zaiyo = inpt[0]; Seizouhou hou = new Seizouhou(); hou.Zairyou = SeihinList[zaiyo - 1]; hou.Count = cnt; SeihinList[makeFor - 1].ZairyouList.Add(hou); } Dictionary ansDic = SeihinList[SeihinList.Count - 1].GetMustBuy(); for (int i = 0; i < SeihinList.Count - 1; i++) { Seihin key = SeihinList[i]; if (ansDic.ContainsKey(key)) { Console.WriteLine(ansDic[key].ToString("###############################0")); } else { Console.WriteLine("0"); } } } private List SeihinList = new List(); public class Seihin { public int SeihinId; public List ZairyouList = new List(); public Dictionary MustBuyList = null; public Dictionary GetMustBuy() { if (MustBuyList != null) { return MustBuyList; } Dictionary ans = new Dictionary(); if (this.ZairyouList.Count > 0) { foreach (Seizouhou seihou in this.ZairyouList) { Dictionary ret = seihou.Zairyou.GetMustBuy(); foreach (Seihin key in ret.Keys) { if (ans.ContainsKey(key)) { ans[key] += (ret[key] * seihou.Count); } else { ans.Add(key, ret[key] * seihou.Count); } } } } else { ans.Add(this, 1); } this.MustBuyList = ans; return ans; } } public class Seizouhou { public Seihin Zairyou; public long Count; } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" 2 1 1 5 2 "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ', bool trim = false) { string inptStr = ReadLine(); if (trim) { inptStr = inptStr.Trim(); } string[] inpt = inptStr.Split(delimiter); int[] ret = new int[inpt.Length]; for (int i = 0; i < inpt.Length; i++) { ret[i] = int.Parse(inpt[i]); } return ret; } } static void Main() { Program prg = new Program(); prg.Proc(); } }