https://atcoder.jp/contests/math-and-algorithm/tasks/typical90_bz
そのままですね。
RustのIteratorにはcountがあるのがいいですね。
// Easy Graph Problem #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// Graph //////////////////// type Node = usize; type Edge = (Node, Node); struct Graph { g: Vec<Vec<Node>> } impl Graph { fn create_from_edges(N: usize, edges: Vec<Edge>) -> Graph { let mut g: Vec<Vec<Node>> = (0..N).map(|_| vec![]).collect(); for (v, w) in edges.into_iter() { g[v].push(w); g[w].push(v) } Graph { g } } } //////////////////// process //////////////////// fn read_input() -> (usize, Vec<Edge>) { let v = read_vec(); let N: usize = v[0]; let M: usize = v[1]; let edges: Vec<Edge> = (0..M).map(|_| read_vec::<Node>()). map(|u| (u[0]-1, u[1]-1)).collect(); (N, edges) } fn is_valid_node(v: Node, graph: &Graph) -> bool { graph.g[v].iter().filter(|w| *w < &v).count() == 1 } fn f(N: usize, edges: Vec<Edge>) -> usize { let graph = Graph::create_from_edges(N, edges); (0..N).filter(|v| is_valid_node(*v, &graph)).count() } fn main() { let (N, edges) = read_input(); println!("{}", f(N, edges)) }