PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#SRD0000090. Nhãn cạnh còn thiếu (Frank Sinatra)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Nhãn cạnh còn thiếu (Frank Sinatra)
Frank Sinatra
Source: Codeforces Gym
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree has vertices numbered from to . Each edge carries a non-negative integer label. Different edges may have the same label.
For every query consisting of two vertices and , consider the unique simple path between them. Find the smallest non-negative integer absent from the labels of the edges on this path.
If , the path contains no edges and the answer is .
Input
- The first line contains , the numbers of vertices and queries.
- Each of the next lines contains : an undirected edge joining and , labelled . These edges form a tree.
- Each of the last lines contains query endpoints .
Output
Print exactly lines. The -th line contains the smallest non-negative integer not found among the edge labels on the path of query .
Subtasks
- Subtask 1 (10 points): , .
- Subtask 2 (15 points): , .
- Subtask 3 (25 points): , .
- Subtask 4 (50 points): , .
Complete conditions for all subtasks, subject to the tighter limits above: , , và .
Examples
Input
7 6
2 1 1
3 1 2
1 4 0
4 5 1
5 6 3
5 7 4
1 3
4 1
2 4
2 5
3 5
3 7
Output
0
1
2
2
3
3
Explanation
The sample edge labels are , , , , , and .
- Path has label set , so
0is missing. - Path has label set , so
1is missing. - Path has labels , so
2is missing. - Path has labels , again giving
2. - Path has labels , giving
3. - Path has labels , also giving
3.