There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (x, y) there is at most one road from x to y.
A path from city s to city t is a sequence of cities p1, p2, ... , pk, where p1 = s, pk = t, and there is a road from city pi to city pi + 1 for each ifrom 1 to k - 1. The path can pass multiple times through each city except t. It can't pass through t more than once.
A path p from s to t is ideal if it is the lexicographically minimal such path. In other words, p is ideal path from s to t if for any other path qfrom s to t pi < qi, where i is the minimum integer such that pi ≠ qi.
There is a tourist agency in the country that offers q unusual excursions: the j-th excursion starts at city sj and ends in city tj.
For each pair sj, tj help the agency to study the ideal path from sj to tj. Note that it is possible that there is no ideal path from sj to tj. This is possible due to two reasons:
- there is no path from sj to tj;
- there are paths from sj to tj, but for every such path p there is another path q from sj to tj, such that pi > qi, where i is the minimum integer for which pi ≠ qi.
The agency would like to know for the ideal path from sj to tj the kj-th city in that path (on the way from sj to tj).
For each triple sj, tj, kj (1 ≤ j ≤ q) find if there is an ideal path from sj to tj and print the kj-th city in that path, if there is any.
The first line contains three integers n, m and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 3000, 1 ≤ q ≤ 4·105) — the number of cities, the number of roads and the number of excursions.
Each of the next m lines contains two integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi), denoting that the i-th road goes from city xi to city yi. All roads are one-directional. There can't be more than one road in each direction between two cities.
Each of the next q lines contains three integers sj, tj and kj (1 ≤ sj, tj ≤ n, sj ≠ tj, 1 ≤ kj ≤ 3000).
In the j-th line print the city that is the kj-th in the ideal path from sj to tj. If there is no ideal path from sj to tj, or the integer kj is greater than the length of this path, print the string '-1' (without quotes) in the j-th line.
7 7 5 1 2 2 3 1 3 3 4 4 5 5 3 4 6 1 4 2 2 6 1 1 7 3 1 3 2 1 3 5
2 -1 -1 2 -1
终于变成Candidate Master了,纪念一波。
题解:给你一个有向图,问任意两点间的字典序最小路径(如果存在)上的第k个节点是啥。
如果不考虑环的情况,那么我们可以把从起点相同的询问放在一起来操作,然后从s点开始按照字典序选边,跑一边dfs,用栈来存储当前的路径。每到一个t点的时候。回答一下<s,t>的询问,即拿出栈中第k个位置的点,这样的话,我们跑n次dfs就可以回答所有的询问了(offline操作)。
现在问题变难了,如果有环的话,字典序最小的路径有可能不存在。这就需要我们在dfs的过程中进行判环,想到使用tarjan算法。
我们每次dfs一个点u的时候,令low[u] = inf;这样的话,如果判断有dfn[u]>=low[u](含义就是出现了一个字典序小的环)那么从经由u到达的其他点的答案均为-1了。
low的更新方法同tarjan。(详细过程请学习一下tarjan)
代码:
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;
const int maxq = 4e5+7;
const int maxn = 3001;
vector<int> edge[maxn];
struct query{int id,x,y,k;}qs[maxq];
vector<query> Q[maxn];
int res[maxq];
int n,m,q;
bool cmp(const query &q1,const query &q2){return q1.x<q2.x;}
int dfn[maxn],low[maxn],instk[maxn],stk[maxn];
int tot,top;
void dfs(int u,int ok){
dfn[u] = ++tot;
low[u] = inf;
stk[top++] = u;
instk[u] = 1;
if(ok)
for(int i = 0;i < Q[u].size();++i)
if(Q[u][i].k <= top) res[Q[u][i].id] = stk[Q[u][i].k-1];
for(int i = 0;i < edge[u].size();++i){
int v = edge[u][i];
if(!dfn[v]){
dfs(v,ok && dfn[u] < low[u]);
low[u] = min(low[v],low[u]);
}
else if(instk[v]) low[u] = min(low[u],dfn[v]);
}
instk[u] = 0;
--top;
}
int main(){
scanf("%d%d%d",&n,&m,&q);
for(int i = 0;i < m;++i){
int a,b;
scanf("%d%d",&a,&b);
edge[a].push_back(b);
}
for(int i = 1;i <= n;i++) sort(edge[i].begin(),edge[i].end());
for(int i = 0;i < q;i++){
int f,t,k;
scanf("%d%d%d",&f,&t,&k);
qs[i] = (query){i,f,t,k};
}
sort(qs,qs+q,cmp);
memset(res,-1,sizeof(res));
for(int i = 0;i < q;++i){
Q[qs[i].y].push_back(qs[i]);
if(qs[i].x != qs[i+1].x){
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(instk,0,sizeof(instk));
tot = top = 0;
dfs(qs[i].x,1);
for(int t = 1;t <= n;++t) Q[t].clear();
}
}
for(int i = 0;i < q;i++) printf("%d\n",res[i]);
return 0;
}
/*
3 4 5
1 3
2 1
3 1
3 2
1 2 1
2 3 2
2 3 3
1 3 1
3 2 1
*/
by 蔡少斐