[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 ...
随机推荐
- es6+最佳入门实践(10)
10.Generator 10.1.Generator是什么? Generator函数是ES6提供的一种异步编程解决方案.在它的内部封装了多个状态,因此,又可以理解为一种状态机,执行Generator ...
- 设置查看java的源程序
1.点 “window”-> "Preferences" -> "Java" -> "Installed JRES" 2. ...
- es修改数据
# 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html#bulk-routing * ...
- django执行sql
http://docs.30c.org/djangobook2/chapter10/ def first_names(self, last_name): cursor = connection.cur ...
- 转: Java安全停止线程方法
转: http://blog.csdn.net/flyingpig4/article/details/7675557 1.早期Java提供java.lang.Thread类型包含了一些列的方法star ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- UVALIVE 2686 Stargates
尼玛真深坑合时p[x] = y 就RE,p[y] = x 就AC . #include <map> #include <set> #include <list> # ...
- php编译中遇到种种error解决办法
http://my.oschina.net/maczhao/blog/365176 编译PHP5.5 make 时出现错误 make: *** [ext/fileinfo/libmagic/appre ...
- POI导入导出小案例
一.HSSF 97-2003 需要jar:poi-3.9.jar 简单示例:生成EXCEL //93---2003 String [] titlie={"id","nam ...
- 一次Ubuntu下的排雷记录
起因 某天,发现一台服务器上出现了一个大量占用cpu资源的进程.尝试手动杀掉,但很快就会自动重新创建新的进程. 追查 用命令lsof -p 10316 查看其文件路径: 该进程文件夹/proc/103 ...