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的序列.你需要将 ...
随机推荐
- JAVA jsp page指令的属性 errorPage 和isErrorPage
>errorPage指定当前页面出现错误的实际响应页面是什么, 其中“/” 表示的是当前WEB应用的根目录 <% page errorPage="/error.jsp" ...
- STM32是如何进入中断服务函数xxx_IRQHandler的
今天在看stm32的中断,一时间不理解stm32主函数是如何进入中断函数的,按C编程的理解,会有个特定的入口之类的,但是看demo过程中没有发现入口. 以串口中断服务函数void USART1_IRQ ...
- [Python]Use Flask-Admin with PostgreSQL
This code recipe gives you an idea of how to use Flask-Admin with postgresql database. from flask im ...
- zzulioj--1827--石锅全拌(区间求和水题)
1827: 石锅全拌 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 6 Solved: 3 SubmitStatusWeb Board Descri ...
- 数论之证明数n等于其因数的欧拉函数值之和
定理: 任何正整数n等于其因数的欧拉函数值之和,即∑d|nφ(d)=n 证明: 设一个集合{1/n,2/n,3/n,...,(n-1)/n,n/n} 对于上述的分式集合,若我们都将其化简至最简形式,设 ...
- Case study: word play
For the exercises in this chapter we need a list of English words. There are lots of word lists avai ...
- 给 “rm” 命令添加个“垃圾桶”
作者: 2daygeek 译者: LCTT amwps290 人类犯错误是因为我们不是一个可编程设备,所以,在使用 rm 命令时要额外注意,不要在任何时候使用 rm -rf *.当你使用 rm 命令时 ...
- Linux系统启动U盘制作工具
1.UNetbootin UNetbootin 让你创建 Ubuntu 或者其他 Linux 发行版的可引导 Live U 盘,而无需烧录 CD. 你既能让 UNetbootin 为你下载众多开箱即用 ...
- 13-Linux中进程与线程的概念以及区别
linux进程与线程的区别,早已成为IT界经常讨论但热度不减的话题.无论你是初级程序员,还是资深专家,都应该考虑过这个问题,只是层次角度不同罢了.对于一般的程序员,搞清楚二者的概念并在工作中学会运用是 ...
- subline 快捷键与功能解释
选择类 Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数 ...