[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 ...
随机推荐
- ionic改tab文字和icon图片的颜色
The official way would be: Change in your theme/variables.scss Active icon for tabs on android is: $ ...
- Codeforces Round #487 (Div. 2) A Mist of Florescence (暴力构造)
C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input stan ...
- HDU 4417 划分树写法
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- 群联MPALL(Rel) 7F V5.03.0A-DL07量产工具 PS2251-07(PS2307)
前言:U盘被写保护,真的很醉人啊~~ 群联MPALL是一款群联PS2251系列主控量产修复工具,本版本支持PS2251-67.PS2251-68.PS2251-02.PS2251-03.PS ...
- Flink源码阅读(一)--Checkpoint触发机制
Checkpoint触发机制 Flink的checkpoint是通过定时器周期性触发的.checkpoint触发最关键的类是CheckpointCoordinator,称它为检查点协调器. org.a ...
- Windows下使用批处理实现启动关闭mysql_DOS/BAT
cls @echo off :设置窗口字体颜色 color 0a :设置窗口标题 TITLE MySQL管理程序 by ThinkVenus call :checkAdmin goto menu :菜 ...
- hdu3294 manacher算法
这道题哇 其实是裸的manacher 无论怎么变 是回文的就是回文 所以 特殊处理一下输出就好了 不过最后的左右端点l,r.l=(p-p[pos]+2)/2-1,r=(p+p[pos]-2)/2-1; ...
- [BZOJ1984]月下“毛景树”解题报告|树链剖分
Description 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里.爬啊爬~爬啊爬~~毛毛虫爬到了一颗小小的“毛景树” ...
- Freewheel Tech interview
1.聊背景.. 2.聊项目..然饿我的项目是webvr..基本面试官很少会了解这个..应该再多做实习多做些项目.. 3.浏览器输入网址后到页面呈现出来的过程 4.缓存机制, 浏览器如何判断一个图片有没 ...
- libssh2
http://www.cnblogs.com/lzrabbit/p/4298794.html shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh ...