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

#BS0000476. Qpwoeirut và các đỉnh (Qpwoeirut and Vertices)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Qpwoeirut và các đỉnh (Qpwoeirut and Vertices)

Qpwoeirut and Vertices

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected connected graph has nn vertices labeled 11 through nn and mm edges numbered 11 through mm. It has neither self-loops nor multiple edges between a pair of vertices.

For each query (l,r)(l,r), only a prefix of the edge list may be used. For an integer kk between 00 and mm, the available edges are precisely those numbered 11 through kk; when k=0k=0, no edges are available.

Find the smallest kk for which every pair of vertices with labels in [l,r][l,r] can reach each other using the available edges. Intermediate vertices of a path may lie outside [l,r][l,r]. Each vertex can reach itself, so a query with l=rl=r has answer 00. Because the graph containing all mm edges is connected, every query has an answer.

Input

  • The first line contains three integers n,m,qn,m,q: the numbers of vertices, edges and queries.
  • Each of the next mm lines contains two integers; line ii gives endpoints ui,viu_i,v_i of edge ii.
  • Each of the final qq lines contains two integers; line jj gives the interval endpoints lj,rjl_j,r_j for query jj.

Output

Print qq space-separated integers on one line, giving the minimum valid kk for each query in input order.

Subtasks

  • Subtask 1 (20 points): 2≤n≤402\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≤lj≤rj≤n1\le l_j\le r_j\le n for every 1≤j≤q1\le j\le q. The undirected graph is connected, without self-loops or multiple edges.
  • Subtask 2 (30 points): 2≤n≤20002\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≤lj≤rj≤n1\le l_j\le r_j\le n for every 1≤j≤q1\le j\le q. The undirected graph is connected, without self-loops or multiple edges.
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 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≤lj≤rj≤n1\le l_j\le r_j\le n for every 1≤j≤q1\le j\le q. The undirected graph is connected, without self-loops or multiple edges.

Examples

Input 1

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

Output 1

3 3 0 5 5

Explanation 1

After the first two edges, vertices 1,2,31,2,3 share a component, while 44 and 55 are separate. Edge 33 links 22 with 44, so all vertices from 11 through 44 first become connected at k=3k=3. At the same time, 33 can reach 44 along 3→1→2→43\to1\to2\to4, so the second answer is also 33.

Interval [2,2][2,2] contains just one vertex and needs no edges. Vertex 55 joins the shared component only at edge 55. Both intervals [2,5][2,5] and [3,5][3,5] include 55 and other vertices, so both answers are 55.

Graph 1

Input 2

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

Output 2

2 2 2 0

Explanation 2

Edge 11 connects only 11 and 33. At edge 22, vertex 22 joins 33 and all three vertices become connected. For interval [1,2][1,2], the route 1→3→21\to3\to2 is valid even though vertex 33 lies outside the queried interval. Since 22 has no incident edge before step 22, the first three answers are all 22. Interval [3,3][3,3] needs no edges, so its answer is 00.

Graph 2