cf 864 F. Cities Excursions

F. Cities Excursions

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 p1, p2, ... , pk, where p1 = s, pk = t, and there is a road from city pi to city pi + 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 pi < qi, where i is the minimum integer such that pi ≠ qi.

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

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

  • there is no path from sj to tj;
  • there are paths from sj to tj, but for every such path p there is another path q from sj to tj, such that pi > qi, where i is the minimum integer for which pi ≠ qi.

The agency would like to know for the ideal path from sj to tj the kj-th city in that path (on the way from sj to tj).

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

Input

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 xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), denoting that the i-th road goes from city xi to city yi. 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 sj, tj and kj (1 ≤ sj, tj ≤ n, sj ≠ tj, 1 ≤ kj ≤ 3000).

Output

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

Example
Input
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
Output
2
-1
-1
2
-1
找字典序最小的路径中,经过的第k个城市,可以采用LCA的处理方式,将查询结果按照分类保存,减少递归次数。题目中可能存在自环。需要特判。Tarjan算法的应用。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <cstdlib>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
struct point{int s,t,k,id;}q[400006];
vector<point>fq[400006];
vector<int>v[3006];
int dnf[3006],low[3006],vis[3006],pos[3006],x,y;
int val[400006],coun,num,n,m,r,k;
bool cmp(point a,point b)
{
    return a.s<b.s;
}
void tarjan(int u,int fa)
{
    dnf[u]=++coun;
    low[u]=INF;
    vis[u]=1;
    pos[num++]=u;
    if(fa)
    {
        for(int i=0;i<fq[u].size();i++)
            if(fq[u][i].k<=num) val[fq[u][i].id]=pos[fq[u][i].k-1];
    }
    for(int i=0;i<v[u].size();i++)
    {
        if(!dnf[v[u][i]])
        {
            tarjan(v[u][i],fa && dnf[u]<low[u]);//防止自环
            low[u]=min(low[v[u][i]],low[u]);
        }
        else if(vis[v[u][i]]) low[u]=min(low[u],dnf[v[u][i]]);
    }
    vis[u]=0;
    --num;
}
int main()
{
    scanf("%d%d%d",&n,&m,&r);
    memset(val,-1,sizeof(val));
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
    }
    for(int i=1;i<=n;i++)
    {
        sort(v[i].begin(),v[i].end());
    }
    for(int i=0;i<r;i++)
    {
        scanf("%d%d%d",&x,&y,&k);
        q[i]=(point){x,y,k,i};
    }
    sort(q,q+r,cmp);
    for(int i=0;i<r;i++)
    {
        fq[q[i].t].push_back(q[i]);
        if(q[i].s!=q[i+1].s)
        {
            coun=num=0;
            memset(dnf,0,sizeof(dnf));
            memset(low,0,sizeof(low));
            memset(vis,0,sizeof(vis));
            tarjan(q[i].s,1);
            for(int j=1;j<=n;j++) fq[j].clear();
        }
    }
    for(int i=0;i<r;i++)
        printf("%d\n",val[i]);
    return 0;
}

 

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7617485.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值