codeforces864F Cities Excursions 图论,tarjan算法的应用

来源:互联网 发布:大亚湾网络问政 编辑:程序博客网 时间:2024/06/05 14:20

F. Cities Excursions
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 p1p2, ... , pk, where p1 = spk = 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 sjtj 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 sjtjkj (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.

Input

The first line contains three integers nm and q (2 ≤ n ≤ 3000,0 ≤ m ≤ 30001 ≤ 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 ≤ nxi ≠ 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 sjtj and kj (1 ≤ sj, tj ≤ nsj ≠ tj1 ≤ kj ≤ 3000).

Output

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.

Example
input
7 7 51 22 31 33 44 55 34 61 4 22 6 11 7 31 3 21 3 5
output
2-1-12-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 51 32 13 13 21 2 12 3 22 3 31 3 13 2 1*/
by 蔡少斐



阅读全文
0 0
原创粉丝点击