CF1706E.Qpwoeirut and Vertices

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a connected undirected graph with nn vertices and mm edges. Vertices of the graph are numbered by integers from 11 to nn and edges of the graph are numbered by integers from 11 to mm.

Your task is to answer qq queries, each consisting of two integers ll and rr. The answer to each query is the smallest non-negative integer kk such that the following condition holds:

  • For all pairs of integers (a,b)(a, b) such that l≤a≤b≤rl\le a\le b\le r, vertices aa and bb are reachable from one another using only the first kk edges (that is, edges 1,2,…,k1, 2, \ldots, k).

给你一个包含 nn 个顶点和 mm 条边的连通无向图。图的顶点用 11 到 nn 的整数编号,图的边用 11 到 mm 的整数编号。

你需要回答 qq 个查询,每个查询由两个整数 ll 和 rr 组成。每个查询的答案是最小的非负整数 kk,使得以下条件成立:

  • 对于所有满足 l≤a≤b≤rl\le a\le b\le r 的整数对 (a,b)(a, b),顶点 aa 和 bb 仅通过前 kk 条边(即边 1,2,…,k1, 2, \ldots, k)即可相互到达。

输入格式

The first line contains a single integer tt (1≤t≤10001\le t\le 1000) — the number of test cases.

The first line of each test case contains three integers nn, mm, and qq (2≤n≤1052\le n\le 10^5, 1≤m,q≤2⋅1051\le m, q\le 2\cdot 10^5) — the number of vertices, edges, and queries respectively.

Each of the next mm lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1\le u_i, v_i\le n) — ends of the ii-th edge.

It is guaranteed that the graph is connected and there are no multiple edges or self-loops.

Each of the next qq lines contains two integers ll and rr (1≤l≤r≤n1\le l\le r\le n) — descriptions of the queries.

It is guaranteed that that the sum of nn over all test cases does not exceed 10510^5, the sum of mm over all test cases does not exceed 2⋅1052\cdot 10^5, and the sum of qq over all test cases does not exceed 2⋅1052\cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001\le t\le 1000)—— 测试用例的数量。

每个测试用例的第一行包含三个整数 nn、mm 和 qq(2≤n≤1052\le n\le 10^5,1≤m,q≤2⋅1051\le m, q\le 2\cdot 10^5)—— 分别表示顶点数、边数和查询数。

接下来的 mm 行中,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1\le u_i, v_i\le n)—— 表示第 ii 条边的两个端点。

保证该图是连通的,且不存在重边或自环。

接下来的 qq 行中,每行包含两个整数 ll 和 rr(1≤l≤r≤n1\le l\le r\le n)—— 表示一次查询。

保证所有测试用例的 nn 之和不超过 10510^5,所有测试用例的 mm 之和不超过 2⋅1052\cdot 10^5,所有测试用例的 qq 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, print qq integers — the answers to the queries.

对于每个测试用例,输出 qq 个整数——即各查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    0 1 
    3 3 0 5 5 
    2

说明/提示

Graph from the first test case. The integer near the edge is its number.

In the first test case, the graph contains 22 vertices and a single edge connecting vertices 11 and 22.

In the first query, l=1l=1 and r=1r=1. It is possible to reach any vertex from itself, so the answer to this query is 00.

In the second query, l=1l=1 and r=2r=2. Vertices 11 and 22 are reachable from one another using only the first edge, through the path 1⟷21 \longleftrightarrow 2. It is impossible to reach vertex 22 from vertex 11 using only the first 00 edges. So, the answer to this query is 11.

Graph from the second test case. The integer near the edge is its number.

In the second test case, the graph contains 55 vertices and 55 edges.

In the first query, l=1l=1 and r=4r=4. It is enough to use the first 33 edges to satisfy the condition from the statement:

  • Vertices 11 and 22 are reachable from one another through the path 1⟷21 \longleftrightarrow 2 (edge 11).
  • Vertices 11 and 33 are reachable from one another through the path 1⟷31 \longleftrightarrow 3 (edge 22).
  • Vertices 11 and 44 are reachable from one another through the path 1⟷2⟷41 \longleftrightarrow 2 \longleftrightarrow 4 (edges 11 and 33).
  • Vertices 22 and 33 are reachable from one another through the path 2⟷1⟷32 \longleftrightarrow 1 \longleftrightarrow 3 (edges 11 and 22).
  • Vertices 22 and 44 are reachable from one another through the path 2⟷42 \longleftrightarrow 4 (edge 33).
  • Vertices 33 and 44 are reachable from one another through the path 3⟷1⟷2⟷43 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4 (edges 22, 11, and 33).

If we use less than 33 of the first edges, then the condition won't be satisfied. For example, it is impossible to reach vertex 44 from vertex 11 using only the first 22 edges. So, the answer to this query is 33.

In the second query, l=3l=3 and r=4r=4. Vertices 33 and 44 are reachable from one another through the path 3⟷1⟷2⟷43 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4 (edges 22, 11, and 33). If we use any fewer of the first edges, nodes 33 and 44 will not be reachable from one another.

第一个测试用例中的图。边旁的整数表示该边的编号。

在第一个测试用例中,图包含 22 个顶点,以及一条连接顶点 11 和 22 的边。

在第一个查询中,l=1l=1 且 r=1r=1。任一顶点均可从自身到达,因此该查询的答案为 00。

在第二个查询中,l=1l=1 且 r=2r=2。仅使用第 11 条边,即可通过路径 1⟷21 \longleftrightarrow 2 实现顶点 11 与 22 之间的相互可达。若仅使用前 00 条边,则无法从顶点 11 到达顶点 22。因此,该查询的答案为 11。

第二个测试用例中的图。边旁的整数表示该边的编号。

在第二个测试用例中,图包含 55 个顶点和 55 条边。

在第一个查询中,l=1l=1 且 r=4r=4。仅需使用前 33 条边,即可满足题目所述条件:

  • 顶点 11 和 22 可通过路径 1⟷21 \longleftrightarrow 2(边 11)相互可达;
  • 顶点 11 和 33 可通过路径 1⟷31 \longleftrightarrow 3(边 22)相互可达;
  • 顶点 11 和 44 可通过路径 1⟷2⟷41 \longleftrightarrow 2 \longleftrightarrow 4(边 11 和 33)相互可达;
  • 顶点 22 和 33 可通过路径 2⟷1⟷32 \longleftrightarrow 1 \longleftrightarrow 3(边 11 和 22)相互可达;
  • 顶点 22 和 44 可通过路径 2⟷42 \longleftrightarrow 4(边 33)相互可达;
  • 顶点 33 和 44 可通过路径 3⟷1⟷2⟷43 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4(边 22、11 和 33)相互可达。

若使用的前若干条边数量少于 33,则无法满足条件。例如,仅使用前 22 条边时,无法从顶点 11 到达顶点 44。因此,该查询的答案为 33。

在第二个查询中,l=3l=3 且 r=4r=4。顶点 33 和 44 可通过路径 3⟷1⟷2⟷43 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4(边 22、11 和 33)相互可达。若使用的前若干条边数量更少,则顶点 33 与 44 将无法相互可达。

输入解题思路,AI测评打分。不知道怎么写?

首页