[Codeforces 864F]Cities Excursions
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 = s, pk = 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 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.
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的更多相关文章
- cf 864 F. Cities Excursions
F. Cities Excursions There are n cities in Berland. Some pairs of them are connected with m directed ...
- 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs
题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...
- [CF864F]Cities Excursions
题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...
- Codeforces Round #436 (Div. 2) 题解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 ...
- Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities
http://codeforces.com/contest/962/problem/E E. Byteland, Berland and Disputed Cities time limit per ...
- Codeforces 665A. Buses Between Cities 模拟
A. Buses Between Cities time limit per test: 1 second memory limit per test: 256 megabytes input: s ...
- Educational Codeforces Round 12 A. Buses Between Cities 水题
A. Buses Between Cities 题目连接: http://www.codeforces.com/contest/665/problem/A Description Buses run ...
- Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
随机推荐
- 学号:201621123032 《Java程序设计》第2周学习总结
1: 本周学习总结 本周学习java的数据类型,两种数据类型:基本数据类型和引用数据类型. 学习关于String和StringBuilder之间不同. 本周还学习数组.一维数组,多维数组,和动态数组. ...
- Error contacting service. It is probably not running.
平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6 hadoop 1.2.1 运行zookeeperd后显示启动成功: JMX enabled ...
- oc中protocol、category和继承的区别
OC中protocol.category和继承的区别以前还是有点迷糊,面试的时候说的有点混乱,现在结合一些资料总结一下. 利用继承,多态是一个很好的保持"对扩展开放.对更改封闭"( ...
- nyoj 概率计算
概率计算 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 A和B两个人参加一场答题比赛.比赛的过程大概是A和B两个人轮流答题,A先答.一旦某人没有正确回答问题,则对手 ...
- python之路--day13-模块
1,什么是模块 模块就是系统功能的集合体,在python中,一个py文件就是一个模块, 例如:module.py 其中module叫做模块名 2,使用模块 2.1 import导入模块 首次带入模块发 ...
- windbg查找Kernel32.dll基址
一.首先准备好一个程序,运行起来,用windbg进行附加调试,由于每个windows下的程序都会加载kernel32.dll,因此,找基址的过程是一样的: 二.查看PEB地址: 法一.r $peb ...
- Linux知识积累(6) 系统目录及其用途
linux系统常见的重要目录以及各个目作用:/ 根目录.包含了几乎所有的文件目录.相当于中央系统.进入的最简单方法是:cd /./boot引导程序,内核等存放的目录.这个目录,包括了在引导过程中所必需 ...
- io使用的设计模式
File f = new File("c:/a.txt"); 1. FileInputStream fis = new FileInputStream(f); 2. Reader ...
- Stanford依存句法关系解释
ROOT:要处理文本的语句 IP:简单从句 NP:名词短语 VP:动词短语 PU:断句符,通常是句号.问号.感叹号等标点符号 LCP:方位词短语 PP:介词短语 CP:由'的'构成的表示修饰性关系的短 ...
- Orm之中介模型
什么是中介模型 中介模型针对的是ManyToMany(多对多)的时候第三张表的问题, 中介模型其实指的就是我们不通过Django创建第三张表,如果自己不创建第三张表,而是由django给我们创建,那就 ...