xnsgraph

Time limit: 5.5s Memory limit: 256MB Input: Output:

Given a tree with NN nodes, numbered from 11 to NN. A partial graph G(V,E)G'(V, E') of the graph G(V,E)G(V, E) is the graph obtained using a subset of edges EE' such that EEE' \subseteq E.

A graph is an xx-graph if there exists an "x", or if KV\exists K \in V such that deg(K)4deg(K) \geq 4.

A graph is an nn-graph if there exists an "n", or if there exists a simple path of at least 44 nodes.

A graph is an ss-graph if there exists an "s", or if there exists a simple path of at least 66 nodes.

A graph is an xnsxns-graph if it satisfies all the conditions above.

Task

You are given QQ queries of the form [l,r][l, r], asking whether the partial graph of the tree formed by the edges in the interval [l,r][l, r] is an xnsxns-graph.

Input data

The first line will contain two integers, NN and QQ, as described above.

Each of the next N1N-1 lines will contain a pair of positive integers aia_i and bib_i, separated by spaces, representing edge ii.

Each of the next QQ lines will contain a pair of positive integers lil_i and rir_i, separated by spaces, representing the ii-th query.

Output data

Line ii should contain the answer corresponding to the ii-th query from the input.

Constraints and clarifications

  • 1N,Q100 0001 \leq N, Q \leq 100 \ 000;
  • 1ai,biN1 \leq a_i, b_i \leq N;
  • 1liriN11 \leq l_i \leq r_i \leq N-1;
  • deg(K)deg(K) denotes the degree of the node KK in the graph;
  • A simple path is a path in which no nodes or edges can appear more than once.
  • The xx, nn, and ss can overlap, i.e., they do not have to be disjoint;
  • Due to the very large input set, it is recommended to use fastio.
  • For tests worth 1010 points: N,Q100N, Q \leq 100;
  • For other tests worth 2020 points: N,Q2 000N, Q \leq 2 \ 000;
  • For other tests worth 2020 points: li=1l_i = 1 for all queries;
  • For other tests worth 1919 points: N,Q60 000N, Q \leq 60 \ 000;
  • For other tests worth 3131 points: no additional constraints.

Example 1

stdin

8 7
1 2
1 3
2 5
2 6
2 4
3 7
7 8
1 1
1 2
1 3
1 4
1 5
1 6
1 7

stdout

0
0
0
0
0
0
1

Explanation

Below you can find the graphical representation of the given tree, where the labels of the edges represent their index (input order).

Log in or sign up to be able to send submissions!