use std::io::Read; fn dfs(v: usize, graph: &Vec>, ans: &mut Vec) -> u32 { if graph[v].len() == 0 { return 1; } if ans[v] > 0 { return ans[v]; } for &(nv, nc) in graph[v].iter() { ans[v] += dfs(nv, &graph, ans) * nc; } ans[v] } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let m: usize = itr.next().unwrap().parse().unwrap(); let mut graph: Vec> = vec![Vec::new(); n]; let mut deg: Vec = vec![0; n]; for _ in 0..m { let p: usize = itr.next().unwrap().parse::().unwrap() - 1; let q: u32 = itr.next().unwrap().parse().unwrap(); let r: usize = itr.next().unwrap().parse::().unwrap() - 1; graph[p].push((r, q)); deg[r] += 1; } let mut ans: Vec = vec![0; n]; for i in 0..n - 1 { if deg[i] == 0 { println!("{}", dfs(i, &graph, &mut ans)); } else { println!("0"); } } }