PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000072. Đếm những gốc cho LCA cho trước (Root LCA Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm những gốc cho LCA cho trước (Root LCA Queries)

Root LCA Queries

Source: CSAcademy

Version: Phuoc Hung OJ Extended

Problem Statement

Given an undirected tree of nn vertices, a query (a,b,c)(a,b,c) considers every possible vertex rr as the root and computes the LCA of aa and bb under root rr. Count how many root choices yield exactly vertex cc. Every qualifying root contributes once. Queries are independent and do not alter the tree.

Input

First line: n qn\ q, then n−1n-1 edges u vu\ v, followed by qq queries a b ca\ b\ c.

Output

Print the number of valid root vertices for each query.

Subtasks

General constraints: 3≤n≤1053\le n\le10^5; 1≤q≤1051\le q\le10^5; 1≤a,b,c≤n1\le a,b,c\le n và a,b,ca,b,c đôi một khác nhau; cạnh tạo cây.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

5 2
1 2
1 3
3 4
3 5
4 5 3
2 4 1

Output

3
1

Explanation

For (4,5,3)(4,5,3), roots 1,2,31,2,3 all produce LCA 33, giving 33 choices. For (2,4,1)(2,4,1) only root 11 qualifies.