[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 ...
随机推荐
- 《统计学习方法》P89页IIS的中间步骤Zw+δ(X)/Zw(X)的推导
共有两个方法:
- [福大软工] W班 软件产品案例分析
作业要求 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1300 评分细则 第一部分 调研,评测 (3 ...
- c语言-第零次作业
1.你认为大学的学习生活.同学关系.师生应该是怎样?请一个个展开描述. 我很荣幸能考进集美大学.集美大学历史悠久.师资力量雄厚.教师与学生素质高.并且集美大学的学习生活和我理想中的一样!首先老师认真负 ...
- C语言字符数组作业
一.PTA实验作业 题目1:7-1 字符串转换成十进制整数 1. 本题PTA提交列表 2. 设计思路 3.代码截图 4.本题调试过程碰到问题及PTA提交列表情况说明. 1.一开始我没想到怎么判断正负的 ...
- djangoueditor 集成xadmin
1.安装Python3兼容版本 https://github.com/twz915/DjangoUeditor3/ 2.model加入字段 from DjangoUeditor.models impo ...
- win7下,使用django运行django-admin.py无法创建网站
安装django的步骤: 1.安装python,选择默认安装在c盘即可.设置环境变量path,值添加python的安装路径. 2.下载ez_setup.py,下载地址:http://peak.tele ...
- zookeeper入门系列:paxos协议
上一章讨论了一种强一致性的情况,即需要分布式事务来解决,本章我们来讨论一种最终一致的算法,paxos算法. paxos算法是由大牛lamport发明的,关于paxos算法有很多趣事.比如lamport ...
- php函数var_dump() 、print_r()、echo()
var_dump() 能打印出类型 print_r() 只能打出值 echo() 是正常输出... 需要精确调试的时候用 var_dump(); 一般查看的时候用 print_r() 另外 , ech ...
- 第三章 jQuery中的事件与动画
第三章jQuery中的事件与动画 一. jQuery中的事件 jQuery事件是对javaScript事件的封装. 1.基础事件 在javaScript中,常用的基础事件有鼠标事件.键盘事件.wind ...
- 微信小程序如何动态增删class类名
简述 由于微信小程序开发不同于以往的普通web开发, 因此无法通过js获取wxml文件的dom结构, 因此从js上直接添加一个类名应该不可能了. 可是我们可以通过微信小程序数据绑定以及view标签的& ...