cf 864 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.
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).
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.
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
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的更多相关文章
- 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs
		
题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...
 - CF 633 F. The Chocolate Spree 树形dp
		
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
 - [Codeforces 864F]Cities Excursions
		
Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...
 - CF #271 F  Ant colony 树
		
题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...
 - CF 494 F. Abbreviation(动态规划)
		
题目链接:[http://codeforces.com/contest/1003/problem/F] 题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空 ...
 - CF 1138 F. Cooperative Game
		
F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...
 - CF 1041 F. Ray in the tube
		
F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...
 - 【Cf #502 F】The Neutral Zone
		
本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...
 - CF 868 F. Yet Another Minimization Problem
		
F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...
 
随机推荐
- UVA 12003 Array Transformer
			
Array Transformer Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Orig ...
 - Mysql 日期型,索引查询的问题
			
问题: 表中,有一个日期字段WorkDate(Date YYYY-MM-DD格式),现在我把它建成了索引,在检索条件时,WorkDate='YYYY-MM-DD' 时,用EXPLAIN分析,能看到使用 ...
 - STL_算法_Heap算法(堆排)(精)
			
C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** STL-算法--Heap算法 堆排序 ...
 - IDEA无法启动:Failed to create JVM:error code -4
			
发生该错误的原因是由于IDEA须要使用的连续内存空间没有得到满足,解决方式: 1.减小-Xmx和-XX:PermSize的值 切换到IDE_HOME\bin\文件夹下,找到<produc ...
 - libevent的使用(socket)
			
这篇文章介绍下libevent在socket异步编程中的应用.在一些对性能要求较高的网络应用程序中,为了防止程序堵塞在socket I/O操作上造成程序性能的下降,须要使用异步编程,即程序准备好读写的 ...
 - BZOJ 3931 Dijkstra+网络流
			
思路: (我能说按照题意模拟么) 用long long inf 要开大--. //By SiriusRen #include <queue> #include <cstdio> ...
 - OpenGL编程(二)绘制矩形
			
上次只是创建了一个简单的窗口,把背景颜色修改为蓝色(默认是黑色),并没有向窗口添加任何图形.这次在上次代码的基础上往窗口中添加一个矩形,设置矩形的颜色,大小等. 1.添加矩形 在(参考上次代码)ren ...
 - ubuntu 18.04网卡命名规则改回传统的ethx
			
自15版本开始网卡命名规则就不叫eth0了.而是用可预期网络接口设备名称的命名规则,比如网卡名为enp3s0 . 如果想要变回ethx也是可以的,参考以下步骤: 1.编辑/etc/default/gr ...
 - 关于thinkphp 命令行
			
很多人做多年开发只懂得PHP能在浏览器下运行或者只能结合APACHE等WEB服务器运行,却不晓得,PHP也能用命令行执行,或许是由于大多人在WINDOWS平台做开发部署运行,比较少接触LINUX. T ...
 - 【Codeforces Round #465 (Div. 2) C】Fifa and Fafa
			
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这个x2,y2和圆心(x1,y1)相连.形成的直线和圆交于点(x3,y3) 则(x2,y2)和(x3,y3)的中点就是所求圆的圆心. ...