use std::collections::VecDeque; fn topologycal_sort(paths: &Vec>) -> Vec { let n = paths.len(); let mut ret = vec![]; let mut ins = vec![0usize; n]; for i in 0..n { for &v in paths[i].iter() { ins[v] += 1; } } let mut que = VecDeque::new(); for i in 0..n { if ins[i] == 0 { que.push_back(i); } } while let Some(x) = que.pop_back() { ret.push(x); for &v in paths[x].iter() { ins[v] -= 1; if ins[v] == 0 { que.push_back(v); } } } ret } fn main() { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let mut paths = vec![vec![]; n]; let mut revpaths = vec![0usize; n]; for _ in 0..m { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let g = temp[0]-1; let mut h = String::new(); std::io::stdin().read_line(&mut h).ok(); let h: Vec = h.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); for &v in h.iter() { let v = v-1; paths[v].push(g); revpaths[g] += 1; } } let mut result = 0; let orders = topologycal_sort(&paths); for &u in orders.iter() { if result >= revpaths[u] { result += 1; } } println!("{}", result); }