import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int n = Integer.parseInt(sa[0]); int q = Integer.parseInt(sa[1]); List> list = new ArrayList<>(n); for (int i = 0; i < n; i++) { list.add(new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { sa = br.readLine().split(" "); int a = Integer.parseInt(sa[0]) - 1; int b = Integer.parseInt(sa[1]) - 1; list.get(a).add(b); list.get(b).add(a); } DoublingLCA dl = new DoublingLCA(list, 0); PrintWriter pw = new PrintWriter(System.out); for (int i = 0; i < q; i++) { sa = br.readLine().split(" "); int s = Integer.parseInt(sa[0]) - 1; int t = Integer.parseInt(sa[1]) - 1; DoublingLCA.Node lca = dl.lca(s, t); int d = dl.nodes[s].dep + dl.nodes[t].dep - lca.dep * 2; if (d % 2 == 0) { pw.println(1); } else { pw.println(0); } } pw.flush(); br.close(); } static class DoublingLCA { int n, h = 1, root; List> list; Node[] nodes; int[][] parent; /** * 初期化 * @param list 隣接リスト * @param root 根の頂点番号 */ public DoublingLCA(List> list, int root) { n = list.size(); this.root = root; this.list = list; while ((1 << h) < n) { h++; } nodes = new Node[n]; for (int i = 0; i < n; i++) { nodes[i] = new Node(i, -1); } parent = new int[h][n]; for (int i = 0; i < h; i++) { Arrays.fill(parent[i], -1); } dfs(root, -1, 0); for (int i = 0; i < h - 1; i++) { for (int j = 0; j < n; j++) { if (parent[i][j] != -1) { parent[i + 1][j] = parent[i][parent[i][j]]; } } } } private void dfs(int x, int p, int dep) { parent[0][x] = p; nodes[x].dep = dep; for (int next : list.get(x)) { if (next != p) { dfs(next, x, dep + 1); } } } /** * 頂点A,BのLCA取得(0≦A,B<N) */ public Node lca(int a, int b) { int u = a; int v = b; if (nodes[u].dep < nodes[v].dep) { u = b; v = a; } for (int i = 0; i < h; i++) { if ((nodes[u].dep - nodes[v].dep >> i & 1) == 1) { u = parent[i][u]; } } if (u == v) { return nodes[u]; } for (int i = h - 1; i >= 0; i--) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return nodes[parent[0][u]]; } public static class Node { int i, dep; public Node(int i, int dep) { this.i = i; this.dep = dep; } } } }