use std::collections::VecDeque; fn main() { let (n, q) = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) }; let ab: Vec<_> = (0..(n - 1)) .map(|_| { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse::().unwrap() - 1, iter.next().unwrap().parse::().unwrap() - 1, ) }) .collect(); let st: Vec<_> = (0..q) .map(|_| { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse::().unwrap() - 1, iter.next().unwrap().parse::().unwrap() - 1, ) }) .collect(); let mut graph = vec![vec![]; n]; for &(a, b) in &ab { graph[a].push(b); graph[b].push(a); } let mut dist = vec![n; n]; dist[0] = 0; let mut queue = VecDeque::from(vec![0]); while let Some(cur) = queue.pop_front() { for &next in &graph[cur] { if dist[cur] + 1 < dist[next] { dist[next] = dist[cur] + 1; queue.push_back(next); } } } for &(s, t) in &st { println!("{}", (dist[s] % 2 == dist[t] % 2) as usize); } }