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[];
vector<point>fq[];
vector<int>v[];
int dnf[],low[],vis[],pos[],x,y;
int val[],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]=;
pos[num++]=u;
if(fa)
{
for(int i=;i<fq[u].size();i++)
if(fq[u][i].k<=num) val[fq[u][i].id]=pos[fq[u][i].k-];
}
for(int i=;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]=;
--num;
}
int main()
{
scanf("%d%d%d",&n,&m,&r);
memset(val,-,sizeof(val));
for(int i=;i<m;i++)
{
scanf("%d%d",&x,&y);
v[x].push_back(y);
}
for(int i=;i<=n;i++)
{
sort(v[i].begin(),v[i].end());
}
for(int i=;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=;i<r;i++)
{
fq[q[i].t].push_back(q[i]);
if(q[i].s!=q[i+].s)
{
coun=num=;
memset(dnf,,sizeof(dnf));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
tarjan(q[i].s,);
for(int j=;j<=n;j++) fq[j].clear();
}
}
for(int i=;i<r;i++)
printf("%d\n",val[i]);
return ;
}

cf 864 F. Cities Excursions的更多相关文章

  1. 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs

    题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...

  2. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

  3. [Codeforces 864F]Cities Excursions

    Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...

  4. CF #271 F Ant colony 树

    题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...

  5. CF 494 F. Abbreviation(动态规划)

    题目链接:[http://codeforces.com/contest/1003/problem/F] 题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空 ...

  6. CF 1138 F. Cooperative Game

    F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...

  7. CF 1041 F. Ray in the tube

    F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...

  8. 【Cf #502 F】The Neutral Zone

    本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...

  9. CF 868 F. Yet Another Minimization Problem

    F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...

随机推荐

  1. solr环境搭建&基本使用

    分步指南 solr服务与tomcat整合 solr使用配置步骤 solr使用 推荐分词工具 相关的文章 一.Solr服务与tomcat整合 1.solr相关版本下载路径:http://archive. ...

  2. [PYTHON]一个简单的单元測试框架

    近期尝试了一下TDD(測试驱动)的模式.感觉效果不错.在此总结一下,同学们假设有更好的办法,一定要告诉我:) 1. 每一个功能模块(文件),配一个单元測试模块. 以手头这个项目为样例:有LogCat. ...

  3. ubuntu 各种窗体操作

    通用 ctrl+alt+0~9 改变窗体大小和是否显示 alt+F4 关闭窗体菜单键+相应启动器位置的编号打开程序 ctrl+pageup/pagedown 在tab间移动 ctrle+shift+p ...

  4. Zepto源代码分析之二~三个API

    因为时间关系:本次仅仅对这三个API($.camelCase.$.contains.$.each)方法进行分析 第一个方法变量转驼峰:$.camelCase('hello-world-welcome' ...

  5. hdu_3308 区间合并

    一两个月没写代码的确是手生的厉害,debug的好艰辛,,不过看到accept时的那种满足感真的就是爽 #include<iostream> #include<cstdio> # ...

  6. 【转】dig详解

    [root@localhost ~]# dig www.a.com ; <<>> DiG 9.2.4 <<>> www.a.com ;; global ...

  7. 制作 Gif 工具

    ScreenToGif:非常小,非常强大: 从此可以十分方便地从视频中抠 gif 出来了: 以及制作一些教学类小 gif,插入到网页中: 丰富的编辑功能: 插入文本,插入标题,插入图像等: 下载地址: ...

  8. SparkCore基础(二)

    * SparkCore基础(二) 继续探讨SparkCore,开门见山,不多废话. SparkApplication结构探讨 包含关系: 之前我们运行过很多App了,其实每一个App都包含若干个Job ...

  9. T-SQL函数类型——系统函数

    1 ??? 为什么 123 和'123'的ISNUMERIC()返回结果相同. SELECT ISNUMERIC(123)  --结果为1SELECT ISNUMERIC('123') --结果为1S ...

  10. CentOS7.4-btrfs管理及使用

    btrfs, B-tree File System, GPL开源文件系统, 支持CoW即读时写入. 核心特性: 多物理卷支持; btrfs可由多个底层磁盘组成 支持RAID mkfs.btrfs 命令 ...