#BS0000474. Ngày kết nối đầu tiên (First Connection Day)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ngày kết nối đầu tiên (First Connection Day)
First Connection Day
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
A region contains cities numbered from to . Initially there are no roads. Over consecutive days, exactly one bidirectional road is opened each day. On day , a road between cities and opens and remains available thereafter. Several roads may connect the same pair of cities.
For each of requests, find the earliest day on which city can reach city using roads opened by the end of that day. If both cities are the same, the answer is . If no such route exists even after day , the answer is .
Input
The first line contains integers , the numbers of cities, road construction days and requests.
The next lines describe the roads: line contains integers , the endpoints of the road opened on day .
Each of the final lines contains integers , the start and destination cities of request .
Output
Print lines, each containing the earliest connection day for the corresponding request, or if the cities never become connected.
Subtasks
- Subtask 1 (20 points): ; ; ; , for every ; for every . Repeated roads are allowed.
- Subtask 2 (30 points): ; ; ; , for every ; for every . Repeated roads are allowed.
- Subtask 3 (50 points): ; ; ; , for every ; for every . Repeated roads are allowed.
Examples
Input
5 4 5
1 2
3 4
2 3
1 4
1 4
1 2
3 3
2 5
3 4
Output
3
1
0
-1
2
Explanation
There are five cities and four construction days.
- Day connects cities and .
- Day connects cities and .
- Day joins the two groups through road . This is the first time a route from to exists, giving answer for the first request.
- Day opens road but does not connect city to any other city.
Cities and are connected on day , so the second answer is . City reaches itself on day , so the third answer is . City stays isolated, giving for the fourth request. Cities and first connect on day , giving the final answer .