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

#PS0000319. Đếm số đường đi qua mỗi cạnh cây (Count Paths Through Edges)

    ID: 3778 Type: Default 5000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 1 Uploaded By: Tags>Tree traversalSubtrees and pathsBinary liftingLowest common ancestorOffline tree algorithms
HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm số đường đi qua mỗi cạnh cây (Count Paths Through Edges)

Count Paths Through Edges

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree has nn vertices numbered from 11 to nn and n−1n-1 edges. Edges are numbered from 11 to n−1n-1 in input order.

There are qq paths. Each is specified by vertices u,vu,v and is the unique simple path joining them. If u=vu=v, the path contains no edges. Repeated paths are counted separately.

For each edge, count how many of the given paths use it.

Input

  • The first line contains integers n,qn,q, the number of vertices and paths, respectively.
  • The next n−1n-1 lines each contain integers xi,yix_i,y_i, the endpoints of edge ii.
  • Each of the last qq lines contains integers u,vu,v, the endpoints of one path.

Output

Print n−1n-1 integers, one per line. Line ii gives the number of paths using edge ii in input order.

Subtasks

  • Subtask 1 (20 points): 2≤n≤2000002\le n\le200000; 1≤q≤2000001\le q\le200000; 1≤xi,yi,u,v≤n1\le x_i,y_i,u,v\le n; 1≤i<n1\le i<n. The edges form a connected undirected tree without duplicate edges or loops. n,q≤100n,q\le100.
  • Subtask 2 (30 points): 2≤n≤2000002\le n\le200000; 1≤q≤2000001\le q\le200000; 1≤xi,yi,u,v≤n1\le x_i,y_i,u,v\le n; 1≤i<n1\le i<n. The edges form a connected undirected tree without duplicate edges or loops; every vertex has degree at most 2.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le200000; 1≤q≤2000001\le q\le200000; 1≤xi,yi,u,v≤n1\le x_i,y_i,u,v\le n; 1≤i<n1\le i<n. The edges form a connected undirected tree without duplicate edges or loops.

Example 1

Input

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

Output

1
2
1

Explanation

The three edges in input order are (1,2),(2,3),(3,4)(1,2),(2,3),(3,4). Path 1→31\to3 uses edges 11 and 22, and path 2→42\to4 uses edges 22 and 33. Edges 11 and 33 are each used once; edge 22 is used twice. Print 11, 22, and 11 in that order.

Example 2

Input

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

Output

3
1
2

Explanation

Edge 1 (3–1) is used by the two occurrences of path 2–3 and by path 3–4. Edge 2 (4–1) is used only by path 3–4. Edge 3 (1–2) is used twice by path 2–3. Path 1–1 uses no edges.