F. Cities Excursions(倍增)

F. Cities Excursions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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
题意就是有一个有向图,然后给你q次询问,每次询问,给你起点和终点,还有一个k,问你从起点到终点的最小字典序的路径上的第k个点是谁,如果不能到达终点输出-1
题目思路:
根据题目要求路径上第k个,我们可以想到倍增,然后我们看题意,从起点到终点要走字典序最小的,那么也就是说任意一条路径他是唯一确定的,那么更加可以用倍增了
还有一个地方要处理,就是怎么处理到达不了的情况,我们很容易知道有两种情况到达不了,就是没有路可以到达终点,还有就是到达终点前有环的情况,那么我们怎么去
处理呢,我们可以把所有询问的的终点存起来,然后枚举终点去离线处理答案,对于每一个终点我们用一个虚点去跟他相连,然后得到每一个可以到达终点的的倍增数组,
然后只用看起点是否能到达那个虚点,如果能就直接去得到第k个点,再看是否为虚点的位置,如果是就增明跑过了,复杂度显然能过
ac代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<sstream>
#include<vector>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 3000+5;
int st[maxn][15];
int head[maxn];
int tot;
struct node
{
    int u,v;
    int net;
}E[maxn];
struct node2
{
    int s,k;
    int id;
};
int vis[maxn];
int n,m,q;
vector<int>E2[maxn];
vector<node2>Q[maxn];
void init()
{
    memset(head,-1,sizeof(head));
    tot = 0;
    for(int i = 0;i<=n;i++){
        Q[i].clear();
    }
    for(int i = 0;i<=n;i++){
        E2[i].clear();
    }
}
void build1(int u,int v)
{
    E[tot].u = u;
    E[tot].v = v;
    E[tot].net = head[u];
    head[u] = tot++;
}
void dfs(int u)
{
    vis[u] = 1;
    for(int i = head[u];~i;i = E[i].net){
        int to = E[i].v;
        if(vis[to]==1) continue;
        dfs(to);
    }
}
int ans[4*100000+50];
int main()
{
    while(~scanf("%d%d%d",&n,&m,&q))
    {
        init();
        for(int i = 0;i<m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            build1(b,a);
            E2[a].push_back(b);
        }
        for(int i = 0;i<q;i++){
            int s,t,k;
            scanf("%d%d%d",&s,&t,&k);
            node2 tmp;
            tmp.s = s;
            tmp.k = k;
            tmp.id = i;
            Q[t].push_back(tmp);
        }
        for(int i = 0;i<=n;i++){
            sort(E2[i].begin(),E2[i].end());
        }
        for(int i = 1;i<=n;i++){
            if(Q[i].size()<=0) continue;
            memset(vis,0,sizeof(vis));
            memset(st,0,sizeof(st));
            dfs(i);
            st[n+1][0] = n+1;
            for(int i = 0;i<13;i++){
                st[n+1][i] = n+1;
            }
            for(int j = 1;j<=n;j++){
                if(vis[j]==1){
                    if(j==i){
                        st[j][0] = n+1;
                        continue;
                    }
                    for(int k = 0;k<E2[j].size();k++){
                        if(vis[E2[j][k]]==1){
                            st[j][0] = E2[j][k];
                            break;
                        }
                    }
                }
            }
            for(int j = 1;j<13;j++){
                for(int k = 1;k<=n;k++){
                    if(vis[k])
                    st[k][j] = st[st[k][j-1]][j-1];

                }
            }
            //for(int j = 1;j<=n;j++)
           //  cout<<j<<' '<<st[j][12]<<endl;
            int len = Q[i].size();
            for(int j = 0 ;j<len;j++)
            {
                node2 tmp = Q[i][j];
                int k = tmp.k-1;
                int s = tmp.s;
                if(vis[s]){
              //          cout<<"fuck"<<' '<<st[s][12]<<endl;
                    if(st[s][12]==n+1){
                for(int w = 0;w<13;w++){
                    if(k&(1<<w)){
                        s = st[s][w];
                    }
                }
                //    cout<<s<<endl;
                    if(s!=n+1){
                        ans[tmp.id] = s;
                    }
                    else{
                        ans[tmp.id] = -1;
                    }
                    }
                    else{
                        ans[tmp.id] = -1;
                    }
                }
                else{
                    ans[tmp.id] = -1;
                }
            }
        }
        for(int i = 0;i<q;i++){
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值