6929.New Roads Queries

Time Limit: 1s Memory Limit: 512MB

There are $n$ cities in Byteland but no roads between them. However, each day, a new road will be built. There will be a total of $m$ roads.

Your task is to process $q$ queries of the form: "after how many days can we travel from city $a$ to city $b$ for the first time?"

Input Format(From the terminal/stdin)

The first input line has three integers $n$ , $m$ and $q$ : the number of cities, roads and queries. The cities are numbered $1,2,\dots,n$ .

After this, there are $m$ lines that describe the roads in the order they are built. Each line has two integers $a$ and $b$ : there will be a road between cities $a$ and $b$ .

Finally, there are $q$ lines that describe the queries. Each line has two integers $a$ and $b$ : we want to travel from city $a$ to city $b$ .

  • $1 \le n, m, q \le 2 \cdot 10^5$
  • $1 \le a,b \le n$

Output Format(To the terminal/stdout)

For each query, print the number of days, or $-1$ if it is never possible.

Sample Input

Copy
5 4 3
1 2
2 3
1 3
2 5
1 3
3 4
3 5
 · · \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
2
-1
4
 \n
  \n
 \n
Source: CSES, Advanced Techniques, 2101

Submit

请先 登录

© 2025 FAQs