CF864F.Cities Excursions

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (x, y) there is at most one road from x to y.

A path from city s to city t is a sequence of cities _p_1, _p_2, ... , p__k, where _p_1 = s, p__k = t, and there is a road from city p__i to city p__i + 1 for each i from 1 to k - 1. The path can pass multiple times through each city except t. It can't pass through t more than once.

A path p from s to t is ideal if it is the lexicographically minimal such path. In other words, p is ideal path from s to t if for any other path q from s to t p__i < q__i, where i is the minimum integer such that p__i ≠ q__i.

There is a tourist agency in the country that offers q unusual excursions: the j-th excursion starts at city s__j and ends in city t__j.

For each pair s__j, t__j help the agency to study the ideal path from s__j to t__j. Note that it is possible that there is no ideal path from s__j to t__j. This is possible due to two reasons:

  • there is no path from s__j to t__j;
  • there are paths from s__j to t__j, but for every such path p there is another path q from s__j to t__j, such that p__i > q__i, where i is the minimum integer for which p__i ≠ q__i.

The agency would like to know for the ideal path from s__j to t__j the k__j-th city in that path (on the way from s__j to t__j).

For each triple s__j, t__j, k__j (1 ≤ j ≤ q) find if there is an ideal path from s__j to t__j and print the k__j-th city in that path, if there is any.

伯兰德有 nn 座城市。其中某些城市对之间由 mm 条有向道路连接。人们只能通过这些道路在城市间移动。不存在连接某城市到其自身的道路。对于任意一对城市 (x,y)(x, y),至多只存在一条从 xx 到 yy 的道路。

从城市 ss 到城市 tt 的一条路径是一个城市序列 p1,p2,…,pkp_1, p_2, \dots, p_k,满足 p1=sp_1 = s、pk=tp_k = t,且对每个 i=1,2,…,k−1i = 1, 2, \dots, k-1,均存在一条从城市 pip_i 到城市 pi+1p_{i+1} 的道路。该路径可多次经过除 tt 外的任意城市,但不能多次经过 tt(即 tt 在路径中至多出现一次)。

从 ss 到 tt 的路径 pp 被称为理想路径,当且仅当它是所有从 ss 到 tt 的路径中字典序最小者。换言之,若 pp 是从 ss 到 tt 的理想路径,则对任意另一条从 ss 到 tt 的路径 qq,设 ii 是满足 pi≠qip_i \ne q_i 的最小下标,则必有 pi<qip_i < q_i。

该国有一家旅游机构,提供 qq 种特殊游览路线:第 jj 种游览路线起始于城市 sjs_j,终止于城市 tjt_j。

对每一对 (sj,tj)(s_j, t_j),请协助该机构找出从 sjs_j 到 tjt_j 的理想路径。注意:从 sjs_j 到 tjt_j 可能不存在理想路径,原因如下两种之一:

  • 不存在从 sjs_j 到 tjt_j 的路径;
  • 存在从 sjs_j 到 tjt_j 的路径,但对任意一条这样的路径 pp,总存在另一条从 sjs_j 到 tjt_j 的路径 qq,使得 pi>qip_i > q_i,其中 ii 是满足 pi≠qip_i \ne q_i 的最小下标。

该机构希望知道:在从 sjs_j 到 tjt_j 的理想路径中(若存在),第 kjk_j 个城市是哪一个(按从 sjs_j 到 tjt_j 的顺序计数)。

对每个三元组 (sj,tj,kj)(s_j, t_j, k_j)(其中 1≤j≤q1 \le j \le q),请判断是否存在从 sjs_j 到 tjt_j 的理想路径;若存在,则输出该路径中第 kjk_j 个城市;否则不输出任何内容。

输入格式

The first line contains three integers n, m and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 3000, 1 ≤ q ≤ 4·105) — the number of cities, the number of roads and the number of excursions.

Each of the next m lines contains two integers x__i and y__i (1 ≤ x__i, y__i ≤ n, x__i ≠ y__i), denoting that the i-th road goes from city x__i to city y__i. All roads are one-directional. There can't be more than one road in each direction between two cities.

Each of the next q lines contains three integers s__j, t__j and k__j (1 ≤ s__j, t__j ≤ n, s__j ≠ t__j, 1 ≤ k__j ≤ 3000).

第一行包含三个整数 nn、mm 和 qq(2 ≤ n ≤ 30002 \leq n \leq 3000,0 ≤ m ≤ 30000 \leq m \leq 3000,1 ≤ q ≤ 4⋅1051 \leq q \leq 4\cdot10^5)——分别表示城市的数量、道路的数量以及远足活动的数量。

接下来的 mm 行中,每行包含两个整数 xix_i 和 yiy_i(1 ≤ xi, yi ≤ n1 \leq x_i, y_i \leq n,xi ≠ yix_i \neq y_i),表示第 ii 条道路从城市 xix_i 指向城市 yiy_i。所有道路均为单向。任意两个城市之间在每个方向上至多只有一条道路。

接下来的 qq 行中,每行包含三个整数 sjs_j、tjt_j 和 kjk_j(1 ≤ sj, tj ≤ n1 \leq s_j, t_j \leq n,sj ≠ tjs_j \neq t_j,1 ≤ kj ≤ 30001 \leq k_j \leq 3000)。

输出格式

In the j-th line print the city that is the k__j-th in the ideal path from s__j to t__j. If there is no ideal path from s__j to t__j, or the integer k__j is greater than the length of this path, print the string '-1' (without quotes) in the j-th line.

在第 jj 行输出从 sjs_j 到 tjt_j 的理想路径中第 kjk_j 个城市。若从 sjs_j 到 tjt_j 不存在理想路径,或整数 kjk_j 大于该路径的长度,则在第 jj 行输出字符串 -1(不带引号)。

输入输出样例

  • 输入#1

    7 7 5
    1 2
    2 3
    1 3
    3 4
    4 5
    5 3
    4 6
    1 4 2
    2 6 1
    1 7 3
    1 3 2
    1 3 5

    输出#1

    2
    -1
    -1
    2
    -1

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

首页