#include using namespace std; #include using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repn(i,a,b) for (int i = (int)(a) ; i < (int)(b+1); i++) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define sz(x) ((int)(x).size()) #define uni(v) v.erase(unique(v.begin(), v.end()), v.end()); using ll = long long; using P = pair; using mint = modint998244353; ostream& operator<< (ostream& os, const mint &x) {os << x.val(); return os;} #ifdef LOCAL #include #define debug(...) debug::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (static_cast(0)) #endif template struct EulerTour { private: struct RMQ { int n, s; using P = std::pair; std::vector

seg; P UNIT = P(1 << 30, -1); RMQ(int N) : n(N), s(1) { while (s < N) s <<= 1; seg.assign(2 * s, UNIT); } void set(int k, P x) { seg[k + s] = x; } P operator[](int k) const { return seg[k + s]; } void build() { for (int k = s - 1; k > 0; k--) { seg[k] = min(seg[2 * k], seg[2 * k + 1]); } } P query(int a, int b) const { P R = UNIT; for (a += s, b += s; a < b; a >>= 1, b >>= 1) { if (a & 1) R = min(R, seg[a++]); if (b & 1) R = min(R, seg[--b]); } return R; } int size() const { return n; } }; std::vector down, up; int id; RMQ rmq; void init(G &g, int root) { dfs(g, root, -1, 0); if (id < rmq.size()) rmq.set(id++, {-1, -1}); for (int i = 0; i < (int)g.size(); i++) { if (down[i] == -1) { rmq.set(id++, {-1, -1}); dfs(g, i, -1, 0); if (id < rmq.size()) rmq.set(id++, {-1, -1}); } } rmq.build(); } void dfs(G &g, int c, int p, int dp) { down[c] = id; rmq.set(id++, {dp, c}); for (auto &d : g[c]) { if (d == p) continue; dfs(g, d, c, dp + 1); } up[c] = id; if (p != -1) rmq.set(id++, {dp - 1, p}); } public: EulerTour(G &g, int root = 0) : down(g.size(), -1), up(g.size(), -1), id(0), rmq(2 * g.size()) { init(g, root); } std::pair idx(int i) const { return {down[i], up[i]}; } int lca(int a, int b) const { if (down[a] > down[b]) std::swap(a, b); return rmq.query(down[a], down[b] + 1).second; } template void node_query(int a, int b, const F &f) { int l = lca(a, b); f(down[l], down[a] + 1); f(down[l] + 1, down[b] + 1); } template void edge_query(int a, int b, const F &f) { int l = lca(a, b); f(down[l] + 1, down[a] + 1); f(down[l] + 1, down[b] + 1); } template void subtree_query(int a, const F &f) { f(down[a], up[a]); } int size() const { return int(rmq.size()); } }; int main(){ int n,q; cin >> n >> q; vector> g(n); rep(_,n-1){ int a,b; cin >> a >> b; a--, b--; g[a].push_back(b); g[b].push_back(a); } EulerTour>> et(g); auto dist = [&](int a, int b){ return et.idx(a).first + et.idx(b).first - 2 * et.idx(et.lca(a,b)).first; }; rep(i,q){ int s,t; cin >> s >> t; s--, t--; int ans = dist(s,t)+1; cout << ans%2 << endl; } return 0; }