PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000475. Truy vấn đường mới (New Roads Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn đường mới (New Roads Queries)

New Roads Queries

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

Byteland has nn cities numbered from 11 to nn. Initially no roads connect the cities. Over the next mm days, one bidirectional road is constructed each day; previously built roads remain available.

You are given the construction order of all mm roads. Two cities can reach each other if a route exists through the roads already built. Each city can also reach itself without using a road. Some pairs may remain disconnected even after all roads are built.

For each of the qq queries (a,b)(a,b), determine the earliest day on which it becomes possible to travel from city aa to city bb. Return -1 if no such day exists.

Input

  • The first line contains three integers n,m,qn,m,q: the numbers of cities, roads and queries.
  • Each of the next mm lines contains two integers; line ii specifies endpoints ui,viu_i,v_i of the road built on day ii.
  • Each of the final qq lines contains two integers; line jj specifies cities aj,bja_j,b_j for query jj.

Output

Print qq lines in query order. Line jj contains the earliest day on which aja_j can reach bjb_j, or -1 if they cannot reach each other even after day mm. If aj=bja_j=b_j, print 00.

Subtasks

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤m≤401\le m\le 40; 1≤q≤401\le q\le 40; 1≤ui,vi≤n1\le u_i,v_i\le n for every 1≤i≤m1\le i\le m; 1≤aj,bj≤n1\le a_j,b_j\le n for every 1≤j≤q1\le j\le q. All roads are bidirectional; endpoints need not differ, endpoint pairs need not be distinct, and the final graph need not be connected.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤m≤20001\le m\le 2000; 1≤q≤20001\le q\le 2000; 1≤ui,vi≤n1\le u_i,v_i\le n for every 1≤i≤m1\le i\le m; 1≤aj,bj≤n1\le a_j,b_j\le n for every 1≤j≤q1\le j\le q. All roads are bidirectional; endpoints need not differ, endpoint pairs need not be distinct, and the final graph need not be connected.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤m≤2000001\le m\le 200000; 1≤q≤2000001\le q\le 200000; 1≤ui,vi≤n1\le u_i,v_i\le n for every 1≤i≤m1\le i\le m; 1≤aj,bj≤n1\le a_j,b_j\le n for every 1≤j≤q1\le j\le q. All roads are bidirectional; endpoints need not differ, endpoint pairs need not be distinct, and the final graph need not be connected.

Examples

Input 1

5 4 3
1 2
2 3
1 3
2 5
1 3
3 4
3 5

Output 1

2
-1
4

Explanation 1

There are 55 cities. Day 11 links 11 and 22; day 22 adds the road between 22 and 33, forming the route 1→2→31\to2\to3. City 33 was not connected to 11 before day 22, so the first answer is 22.

City 44 remains isolated through all four days, giving -1 for the second query. Day 44 adds road 2−52-5, making the route 3→2→53\to2\to5 possible. City 55 was isolated until then, so the final answer is 44.

Input 2

4 4 4
1 1
1 2
2 1
3 4
1 1
1 2
1 4
4 3

Output 2

0
2
-1
4

Explanation 2

City 11 can reach itself on day 00. The first road does not link distinct cities. Day 22 links 11 and 22, giving 22 for pair (1,2)(1,2). Day 33 repeats an existing connection. Day 44 connects 33 and 44, but the groups {1,2}\{1,2\} and {3,4}\{3,4\} remain separate. The last two answers are therefore -1 and 44.