[Codeforces 864F]Cities Excursions

时间:2022-03-10 21:34:57

Description

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 = spk = t, and there is a road from city pi to city pi + 1 for each ifrom 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 qfrom 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 sjtj 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 sjtjkj (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 nm 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 ≤ nxi ≠ 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 sjtj and kj (1 ≤ sj, tj ≤ nsj ≠ 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.

Sample 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

Sample Output

2
-1
-1
2
-1

题解

最后一题竟然耗了(沉迷B站无法自拔)三个小时。

给出一个有向图。对于每个询问,你需要去找到从$s_i$到$t_i$字典序最小的路径上第$k_i$个点。

首先按终点$t_i$分组,找到所有和$t_i$连通的节点。可以通过将所有的边反向并且从$t_i$开始$dfs$来实现。

现在,我们考虑询问$(s_i,t_i)$。对于这组询问,你需要去找到从$s_i$到$t_i$字典序最小的路径。如果节点$t_i$与$s_i$不连通,那么答案就是'$-1$'。值得注意的是,如果字典序最小的路径成为一个环,那么答案也是'$-1$'。

因此,我们建一个新图包括原图的所有的反向边。枚举所有的终点$t$,从$t$遍历整个图,取前驱节点最优路径,发现所有与$t$相连的点构成了一棵外向树,那么这些在树上的点与根节点之间的路径就是题目要求的字典序最小的路径。(注意是在“树”上,若形成环显然不行)

接着我们可以用倍增来找从$s$开始的第$k$个节点。

 //It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = ;
const int Q = 4e5;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int n, m, q, u, v, k;
int lim, f[N+][];
int ans[Q+];
struct tt {
int to, next, k, id;
}edge[N+], query[Q+];
int path_e[N+], top_e;
int path_q[N+], top_q; void add_e(int u, int v) {
edge[++top_e].to = v;
edge[top_e].next = path_e[u];
path_e[u] = top_e;
}
void add_q(int u, int v, int k, int id) {
query[++top_q].to = v;
query[top_q].next = path_q[u];
query[top_q].k = k;
query[top_q].id = id;
path_q[u] = top_q;
}
void dfs(int u, int fa) {
for (int i = path_e[u]; i; i = edge[i].next)
if (edge[i].to != fa && (f[edge[i].to][] == || f[edge[i].to][] > u))
f[edge[i].to][] = u, dfs(edge[i].to, fa);
}
void work() {
read(n), read(m), read(q);
lim = log(n)/log()+;
for (int i = ; i <= m; i++) {
read(u), read(v);
add_e(v, u);
}
for (int i = ; i <= q; i++) {
read(u), read(v), read(k);
add_q(v, u, k, i);
}
for (int i = ; i <= n; i++) {
memset(f, , sizeof(f));
dfs(i, i);
for (int t = ; t <= lim; t++)
for (int j = ; j <= n; j++)
f[j][t] = f[f[j][t-]][t-];
for (int j = path_q[i]; j; j = query[j].next) {
int u = query[j].to, id = query[j].id, k = query[j].k-;
if (f[u][lim] || (!f[u][])) {
ans[id] = -;
continue;
}
for (int t = ; k; k >>= , t++)
if (k&) u = f[u][t];
ans[id] = u ? u : -;
}
}
for (int i = ; i <= q; i++)
printf("%d\n", ans[i]);
}
int main() {
work();
return ;
}

[Codeforces 864F]Cities Excursions的更多相关文章

  1. cf 864 F&period; Cities Excursions

    F. Cities Excursions There are n cities in Berland. Some pairs of them are connected with m directed ...

  2. 【做题】Codeforces Round &num;436 &lpar;Div&period; 2&rpar; F&period; Cities Excursions——图论&plus;dfs

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

  3. &lbrack;CF864F&rsqb;Cities Excursions

    题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...

  4. Codeforces Round &num;436 &lpar;Div&period; 2&rpar; 题解864A 864B 864C 864D 864E 864F

    A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Educational Codeforces Round 42 &lpar;Rated for Div&period; 2&rpar; E&period; Byteland&comma; Berland and Disputed Cities

    http://codeforces.com/contest/962/problem/E E. Byteland, Berland and Disputed Cities time limit per ...

  6. Codeforces 665A&period; Buses Between Cities 模拟

    A. Buses Between Cities time limit per test: 1 second memory  limit per test: 256 megabytes input: s ...

  7. Educational Codeforces Round 12 A&period; Buses Between Cities 水题

    A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...

  8. Codeforces C&period; Jzzhu and Cities(dijkstra最短路)

    题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

随机推荐

  1. CentOS7下安装和使用Xdebug

    wget http://xdebug.org/files/xdebug-2.4.0rc4.tgztar xvzf xdebug-2.4.0rc4.tgzcd xdebug-2.4.0RC4phpize ...

  2. 杂谈&colon;Servlet&lpar;2&rpar;

    Servlet的方法剖析: 1.service()方法里面做了什么? 2.doGet()与doPost()做了什么?应该怎么写? 回答 1.service()方法里面做了什么? 如果你的service ...

  3. FileStream的使用

    一.写入文件 string strContent = textBox2.Text.ToString(); //创建文件流(文件路径,文件操作,创建) using (FileStream fs = ne ...

  4. IOS 播放动态Gif图片

    图片分为静态和动态两种,图片的格式有很多种,在开发中比较常见的是.png和.jpg的静态图片,但有的时候在App中需要播放动态图片,比如.gif格式的小表情头像,在IOS中并没有提供直接显示动态图片的 ...

  5. Hibernate学习之缓存简析

    一.一级缓存 Hibernate的Session提供了一级缓存的功能,默认总是有效的,当应用程序保存持久化实体.修改持久化实体时,Session并不会立即把这种改变提交到数据库,而是缓存在当前的Ses ...

  6. python:面向对象初级

    面向对象编程类的概念 : 具有相同属性和技能的一类事物 人类 抽象对象 : 就是对一个类的具体的描述 具体的人 具体 使用面向对象的好处: 使得代码之间的角色关系更加明确 增强了代码的可扩展性 规范了 ...

  7. asp&period;net core mvc上传大文件解决方案

    默认上传文件大小不超过30M 第一个问题: IIS 10.0 详细错误 - 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 服务器上的请求筛选被配置为拒绝该请求 ...

  8. 如何运行ruby代码

    第一种,ruby -e 在命令行中运行下面命令,-e的意思是,把后面的字符串当作脚本执行 ruby -e "print 'hello'" 使用irb交互控制台 在命令行输入irb ...

  9. 7&period;24 IO多路复用和协程代码笔记

    1. 复习 # !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/23 11:49 # !@Author TrueNew ...

  10. 在spring MVC 中关于session失效的判断 有一个类SessionStatus

    SessionStatus status 表示的是当前Session的状态  status.isComplete()-->为true时,表示当前Session还未过期;-->false,表 ...