[CF864F]Cities Excursions
题目大意:
一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点)。若存在,求出该路径上经过的第$k_i$个结点。
思路:
将原图的边反向。考虑根据$t_i$对所有询问进行分组。对于$t_i$相同的询问,在反向图中DFS,求出每个结点到$t_i$的最小字典序路径中的下一个结点是多少,这可以转化为一个树形结构。若$s_i$与$t_i$不连通,则说明路径不存在;若$s_i$的第$2^{\lfloor\log_2n\rfloor+1}$级祖先存在,则说明存在环。询问第$k_i$个结点可以树上倍增。
#include<cstdio>
#include<cctype>
#include<cstring>
#include<forward_list>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
constexpr int N=,Q=4e5,logN=;
std::forward_list<int> e[N];
struct Query {
int s,k,id;
};
std::forward_list<Query> q[N];
int ans[Q],anc[N][logN];
inline int lg2(const float &x) {
return ((unsigned&)x>>&)-;
}
void dfs(const int &x,const int &par,const int &s) {
anc[x][]=par;
for(auto &y:e[x]) {
if(y==s||(anc[y][]&&anc[y][]<=x)) continue;
dfs(y,x,s);
}
}
int main() {
const int n=getint(),m=getint(),cnt_q=getint(),lim=lg2(n)+;
for(register int i=;i<m;i++) {
const int u=getint(),v=getint();
e[v].push_front(u);
}
for(register int i=;i<cnt_q;i++) {
const int s=getint(),t=getint(),k=getint();
q[t].push_front({s,k-,i});
}
for(register int i=;i<=n;i++) {
if(q[i].empty()) continue;
memset(anc,,sizeof anc);
dfs(i,,i);
for(register int j=;j<=lim;j++) {
for(register int i=;i<=n;i++) {
anc[i][j]=anc[anc[i][j-]][j-];
}
}
for(register auto &j:q[i]) {
if(!anc[j.s][]||anc[j.s][lim]) continue;
for(register int i=;j.k;j.k>>=,i++) {
if(j.k&) j.s=anc[j.s][i];
}
ans[j.id]=j.s;
}
}
for(register int i=;i<cnt_q;i++) {
printf("%d\n",ans[i]?:-);
}
return ;
}
[CF864F]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 864F]Cities Excursions
Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...
- 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs
题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到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 ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- codeforces 613D:Kingdom and its Cities
Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...
- CF449B Jzzhu and Cities (最短路)
CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- 使用MAT分析内存泄露
使用MAT分析内存泄露 对于大型服务端应用程序来说,有些内存泄露问题很难在测试阶段发现,此时就需要分析JVM Heap Dump文件来找出问题.随着单机内存越来越大,应用heap也开得越来越大,动辄十 ...
- 配置Tomcat时server.xml和content.xml自动还原问题
当我们在处理中文乱码或是配置数据源时,我们要修改Tomcat下的server.xml和content.xml文件. 但是当我们修改完后重启Tomcat服务器时发现xml文件又被还原了,修改无效果. 为 ...
- Bash 实例,第二部分
我们先看一下处理命令行自变量的简单技巧,然后再看看 bash 基本编程结构. 接收自变量 在 介绍性文章 中的样本程序中,我们使用环境变量 "$1" 来引用第一个命令行自变量.类似 ...
- Java之戳中痛点 - (2)取余用偶判断,不要用奇判断
取余判断原则:取余用偶判断,不要用奇判断 先看一个 程序: package com.test; import java.util.Scanner; public class t1 { public s ...
- struts2学习笔记(二)
一. 国际化的目标 1). 如何配置国际化资源文件 I. Action 范围资源文件: 在Action类文件所在的路径建立名为 ActionName_language_country.properti ...
- python3 内置函数(转)
http://www.runoob.com/python/python-built-in-functions.html divmod(7,2) # 返回(3,1)商和余的元组 frozenset() ...
- 别再滥用scrapy CrawlSpider中的follow=True
对于刚接触scrapy的同学来说, crawlspider中的rule是比较难理解的, 很可能驾驭不住. 而且笔者在YouTube中看到许多公开的演讲都都错用了follow这一选项, 所以今天就来仔细 ...
- centos7安装tengine强制使用HTTPS访问
操作系统:centos7.2 x64tengine:Tengine/2.2.0主机IP: 10.0.0.12 一.安装tengine 1.1 下载源码安装包 1.1.1 源码包pcre-8.40 ...
- WAB QQ第三方登录
应用场景 web应用通过QQ登录授权实现第三方登录. 操作步骤 1 注册成为QQ互联平台开发者,http://connect.qq.com/ 2 准备一个可访问的域名, ...
- JS:body元素对象的clientWidth、offsetWidth、scrollWidth、clientLeft、offsetLeft、scrollLeft
document.body.clientWidth 获取body元素对象的内容可视区域的宽度,即clientWidth=width+padding,不包括滚动条. document.body.clie ...