[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 ...
随机推荐
- SCOI2005 互不侵犯 [状压dp]
题目传送门 题目大意:有n*n个格子,你需要放置k个国王使得它们无法互相攻击,每个国王的攻击范围为上下左走,左上右上左下右下,共8个格子,求最多的方法数 看到题目,是不是一下子就想到了玉米田那道题,如 ...
- 7月19日day11总结
今天学习过程和小结 上午进行测试复习了 1,hdfs中namenode和datanode作用 2,hdfs副本存放机制 3,mapreduce计算处理过程 4,格式化hdfs命令 5,hdfs的核心配 ...
- Java之戳中痛点 - (1)易变业务使用脚本语言编写
脚本语言的3大特征: 1.灵活:脚本语言一般是动态类型,可以不声明变量类型直接使用,也可以在运行期改变类型:2.便捷:脚本语言是解释性语言,在运行期变更非常方便,而不用重启服务3.简单:脚本语言语法比 ...
- CentOS 6.5 Linux 安装 openoffice
资源准备: Apache_OpenOffice_4.1.4_Linux_x86-64_install-rpm_zh-CN.tar.gz 编译安装: 本人资源包放在 /opt/moudles 中, 解压 ...
- Spring学习--基于 XML 的配置声明切面
正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...
- Java基础学习知识体系图
- [BZOJ3275]Number解题报告|网络流
Description 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c*c2:gcd(a,b)=1 这道 ...
- CocoaPods详解之----使用篇
http://blog.csdn.net/meegomeego/article/details/24005567 作者:wangzz 原文地址:http://blog.csdn.net/wzzvict ...
- 【FIRST USE】第一次用git把代码上传到github
第一次使用某个东西总是充满了伤痛,我第一次用sed的时候,毁掉了我的所有源代码,第一次用git的时候一直提示不正确,后来解决了问题,便记录下来. 首先说明我的环境,我用的是虚拟机,上面运行的64位的c ...
- servlet(4) - servletAPI - 小易Java笔记
Servlet规范核心类图 1.请求和响应对象 ==> HTTP协议包含请求和响应部分. ==> HttpServletRequest就代表着请求部分 ==> HttpServlet ...