CF864F.Cities Excursions

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

There are nn cities in Berland. Some pairs of them are connected with mm 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)(x,y) there is at most one road from xx to yy .

A path from city ss to city tt is a sequence of cities p1p_{1} , p2p_{2} , ... , pkp_{k} , where p1=sp_{1}=s , pk=tp_{k}=t , and there is a road from city pip_{i} to city pi+1p_{i+1} for each ii from 11 to k1k-1 . The path can pass multiple times through each city except tt . It can't pass through tt more than once.

A path pp from ss to tt is ideal if it is the lexicographically minimal such path. In other words, pp is ideal path from ss to tt if for any other path qq from ss to tt pi<qip_{i}<q_{i} , where ii is the minimum integer such that piqip_{i}≠q_{i} .

There is a tourist agency in the country that offers qq unusual excursions: the jj -th excursion starts at city sjs_{j} and ends in city tjt_{j} .

For each pair sjs_{j} , tjt_{j} help the agency to study the ideal path from sjs_{j} to tjt_{j} . Note that it is possible that there is no ideal path from sjs_{j} to tjt_{j} . This is possible due to two reasons:

  • there is no path from sjs_{j} to tjt_{j} ;
  • there are paths from sjs_{j} to tjt_{j} , but for every such path pp there is another path qq from sjs_{j} to tjt_{j} , such that pi>qip_{i}>q_{i} , where ii is the minimum integer for which piqip_{i}≠q_{i} .

The agency would like to know for the ideal path from sjs_{j} to tjt_{j} the kjk_{j} -th city in that path (on the way from sjs_{j} to tjt_{j} ).

For each triple sjs_{j} , tjt_{j} , kjk_{j} ( 1<=j<=q1<=j<=q ) find if there is an ideal path from sjs_{j} to tjt_{j} and print the kjk_{j} -th city in that path, if there is any.

输入格式

The first line contains three integers nn , mm and qq ( 2<=n<=30002<=n<=3000 , 0<=m<=30000<=m<=3000 , 1<=q<=41051<=q<=4·10^{5} ) — the number of cities, the number of roads and the number of excursions.

Each of the next mm lines contains two integers xix_{i} and yiy_{i} ( 1<=xi,yi<=n1<=x_{i},y_{i}<=n , xiyix_{i}≠y_{i} ), denoting that the ii -th road goes from city xix_{i} to city yiy_{i} . All roads are one-directional. There can't be more than one road in each direction between two cities.

Each of the next qq lines contains three integers sjs_{j} , tjt_{j} and kjk_{j} ( 1<=sj,tj<=n1<=s_{j},t_{j}<=n , sjtjs_{j}≠t_{j} , 1<=kj<=30001<=k_{j}<=3000 ).

输出格式

In the jj -th line print the city that is the kjk_{j} -th in the ideal path from sjs_{j} to tjt_{j} . If there is no ideal path from sjs_{j} to tjt_{j} , or the integer kjk_{j} is greater than the length of this path, print the string '-1' (without quotes) in the jj -th line.

输入输出样例

  • 输入#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
    
首页