#BS0000477. Hành trình sưu tập dấu (Stamp Rally)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hành trình sưu tập dấu (Stamp Rally)
Stamp Rally
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
Consider a connected undirected graph with vertices numbered from to . Its edges are numbered in input order; edge joins and .
There are pairs of brothers collecting stamps. In pair , the brothers begin at vertices and , respectively. Each may walk along graph edges. Together they must visit exactly distinct vertices, including their starting vertices. Repeated visits to a vertex, even by both brothers, count only once.
The score of a journey is the highest number of any edge used by either brother. Determine the smallest achievable score for each pair.
Input
The first line contains integers , the numbers of vertices and edges.
The next lines give the edges: line contains integers , the endpoints of edge .
The following line contains the integer , the number of pairs.
Each of the final lines contains integers , the starting vertices and required number of distinct visited vertices for pair .
Output
Print lines. Line must contain the minimum integer score for pair .
Subtasks
- Subtask 1 (20 points): ; ; ; for every ; , for every . The graph is connected; repeated edges are allowed.
- Subtask 2 (30 points): ; ; ; for every ; , for every . The graph is connected; repeated edges are allowed.
- Subtask 3 (50 points): ; ; ; for every ; , for every . The graph is connected; repeated edges are allowed.
Examples
Input
5 4
1 2
3 4
2 3
4 5
4
1 3 3
1 3 4
1 3 5
1 2 3
Output
1
2
4
3
Explanation
The graph contains five vertices and edges , , , in that order.
- For , edge allows the brother at vertex to reach , while the other remains at . With no edge only two vertices are visited; the minimum score is .
- For , edge also permits a visit to vertex . The first edge alone allows only three vertices, so the answer is .
- For , vertex stays isolated until edge is available. With that edge all five vertices can be visited, so the answer is .
- For , neither of the first two edges provides access to a third vertex from the starting vertices. Edge gives access to vertex , so the answer is .