题目大意:
  一个$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的更多相关文章

  1. cf 864 F. Cities Excursions

    F. Cities Excursions There are n cities in Berland. Some pairs of them are connected with m directed ...

  2. [Codeforces 864F]Cities Excursions

    Description There are n cities in Berland. Some pairs of them are connected with m directed roads. O ...

  3. 【做题】Codeforces Round #436 (Div. 2) F. Cities Excursions——图论+dfs

    题意:给你一个有向图,多次询问从一个点到另一个点字典序最小的路径上第k个点. 考虑枚举每一个点作为汇点(记为i),计算出其他所有点到i的字典序最小的路径.(当然,枚举源点也是可行的) 首先,我们建一张 ...

  4. 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 ...

  5. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  6. codeforces 613D:Kingdom and its Cities

    Description Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. Ho ...

  7. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  8. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. Supermarket [堆]

    Supermarket 题目描述 有一个商店有许多批货,每一批货又有N(0<=N<=\(10^4\))个商品,同时每一样商品都有收益Pi​ ,和过期时间Di​ (1<=Pi,Di&l ...

  2. 获得edittext的图片大小

    1.在布局文件中编写控件,有2张图片 <EditText android:id="@+id/edit" android:background="@drawable/ ...

  3. eclipse console输出有长度限制

    抓取一个网页内容,然后打印到控制台,发现内容首部都没有了. String content = getResponseText("http://xxx.html"); System. ...

  4. ACM-ICPC 2018 南京赛区网络预赛 Sum

    A square-free integer is an integer which is indivisible by any square number except 11. For example ...

  5. 【CodeForces】841C. Leha and Function(Codeforces Round #429 (Div. 2))

    [题意]定义函数F(n,k)为1~n的集合中选择k个数字,其中最小数字的期望. 给定两个数字集A,B,A中任意数字>=B中任意数字,要求重组A使得对于i=1~n,sigma(F(Ai,Bi))最 ...

  6. 记一次CUDA编程任务

    这个月6号开始,着手解决一个具有实际意义的计算任务.任务数据有9879896条,每条包含30个整数,任务是计算每两条数据之间的斯皮尔相关系数及其P值.原始数据只有500+MB,因此我并不认为这是个多么 ...

  7. POJ 1698 Alice's Chance

    题目:Alice 要拍电影,每一天只能参与一部电影的拍摄,每一部电影只能在 Wi 周之内的指定的日子拍摄,总共需要花 Di 天时间,求能否拍完所有电影. 典型的二分图多重匹配,这里用了最大流的 din ...

  8. vmware的3种网络模式

    ####图片以及部分内容来源:https://note.youdao.com/share/?id=236896997b6ffbaa8e0d92eacd13abbf&type=note#/ 在安 ...

  9. python多线程实现多任务

    #转载请联系 1.什么是线程? 进程是操作系统分配程序执行资源的单位,而线程是进程的一个实体,是CPU调度和分配的单位.一个进程肯定有一个主线程,我们可以在一个进程里创建多个线程来实现多任务. --- ...

  10. [BZOJ1176][Balkan2007]Mokia cdq+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 3134  Solved: 1395[Submit][S ...