#PS0000319. Đếm số đường đi qua mỗi cạnh cây (Count Paths Through Edges)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đế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 vertices numbered from to and edges. Edges are numbered from to in input order.
There are paths. Each is specified by vertices and is the unique simple path joining them. If , 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 , the number of vertices and paths, respectively.
- The next lines each contain integers , the endpoints of edge .
- Each of the last lines contains integers , the endpoints of one path.
Output
Print integers, one per line. Line gives the number of paths using edge in input order.
Subtasks
- Subtask 1 (20 points): ; ; ; . The edges form a connected undirected tree without duplicate edges or loops. .
- Subtask 2 (30 points): ; ; ; . The edges form a connected undirected tree without duplicate edges or loops; every vertex has degree at most 2.
- Subtask 3 (50 points): ; ; ; . 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 . Path uses edges and , and path uses edges and . Edges and are each used once; edge is used twice. Print , , and 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.