codeforces864F Cities Excursions 图论,tarjan算法的应用
来源:互联网 发布:大亚湾网络问政 编辑:程序博客网 时间:2024/06/05 14:20
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 51 22 31 33 44 55 34 61 4 22 6 11 7 31 3 21 3 5
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 蔡少斐
- codeforces864F Cities Excursions 图论,tarjan算法的应用
- [codeforces864F]Cities Excursions
- Codeforces 864F Cities Excursions(离线处理+Tarjan)
- Codeforces Round 864F (Codeforces Round #436 Div. 2) F. Cities Excursions tarjan判环
- Codeforces Round #436 (Div. 2) F. Cities Excursions 字典序dfs+Tarjan判环
- CodeForces 864F Cities Excursions
- F. Cities Excursions(倍增)
- 关于tarjan算法的应用
- Codeforces Round #436 (Div. 2) F. Cities Excursions 路径上的第k个点(倍增)
- HDU 2874 - Connections between cities(LCA‘离线算法Tarjan)
- Codeforces Round #436 (Div. 2) F. Cities Excursions
- hdu 3836,tarjan算法的应用(有向图缩点)
- poj-2186 受欢迎的奶牛(tarjan算法应用)
- 强连通分量的tarjan算法应用(一)
- Tarjan 的应用
- hdu 2874 Connections between cities 最近公共祖先lca(离线算法/tarjan算法)
- hdu2874Connections between cities【LCA tarjan】
- Tarjan算法的改装
- 论文翻译(1)FaceNet: A Unified Embedding for Face Recognition and Clustering
- IO流(一)和深入递归
- Web Angular用户密码强度判别
- LeetCode:83.Remove Duplicates from Sorted List
- python爬虫框架scrapy实例详解
- codeforces864F Cities Excursions 图论,tarjan算法的应用
- Java多线程学习(吐血超详细总结)
- FPGA在其他领域的应用(二)
- 快速搭建ssh框架--ideamac版
- 构造方法传参数
- 软件测试(一)
- 深入浅出朴素贝叶斯法(Naive Bayes)
- Android基础入门1
- IO流(二)和异常处理