POJ 1986 Distance Queries LCA离线算法tarjan
来源:互联网 发布:怎样走网络群众路线 编辑:程序博客网 时间:2024/06/05 15:22
Distance Queries
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 13792 Accepted: 4870Case Time Limit: 1000MS
Description
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input
* Lines 1..1+M: Same format as "Navigation Nightmare"
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
7 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S31 61 42 6
Sample Output
13336
Hint
Farms 2 and 6 are 20+3+13=36 apart.
解题思路:
1,输入的就是一个树,n个点m个边,后面的输入的字母没发现什么含义,可能就是迷惑人的,忽视掉。
2,LCA的tarjan算法,这个算法网上到处都有,//其实只看代码也看得懂,挺简单的思路
#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 100005 ;int n,m;struct Edge{ int from,to,dist ;};vector<Edge> edges ;vector<int> G[maxn] ;void add_edge(int from,int to,int dist){ edges.push_back((Edge){from,to,dist}) ; int mm = edges.size() ; G[from].push_back(mm-1) ;}vector<Edge> edges2 ;vector<int> G2[maxn] ;void add_edge2(int from,int to,int dist=0){ edges2.push_back((Edge){from,to,dist}) ; int mm = edges2.size() ; G2[from].push_back(mm-1) ;}int f[maxn] ;int find(int x){ return x==f[x]?x:f[x]=find(f[x]) ;}bool vis[maxn] ;int len[maxn] ;void LCA(int u){ f[u] = u ; vis[u] = true ; for(int i=0;i<G[u].size();i++){ if(!vis[edges[G[u][i]].to]){ len[edges[G[u][i]].to] = len[u]+edges[G[u][i]].dist ; LCA(edges[G[u][i]].to) ; f[edges[G[u][i]].to] = u ; } } for(int i=0;i<G2[u].size();i++){ if(vis[edges2[G2[u][i]].to]){ edges2[G2[u][i]^1].dist = edges2[G2[u][i]].dist = len[edges2[G2[u][i]].to]+len[u]-2*len[find(edges2[G2[u][i]].to)] ; } }}int main(){ while(~scanf("%d%d",&n,&m)){ memset(G,0,sizeof(G)) ; memset(G2,0,sizeof(G2)) ; memset(f,0,sizeof(f)) ; memset(vis,false,sizeof(vis)) ; memset(len,0,sizeof(len)) ; edges.clear() ; edges2.clear() ; for(int i=0;i<n-1;i++){ int u,v,w; char c[2]; scanf("%d%d%d%s",&u,&v,&w,c); add_edge(u,v,w); add_edge(v,u,w); } int k; scanf("%d",&k) ; for(int i=0;i<k;i++){ int u,v; scanf("%d%d",&u,&v); add_edge2(u,v); add_edge2(v,u); } LCA(1) ; for(int i=0;i<k;i++){ printf("%d\n",edges2[2*i].dist) ; } } return 0;}
阅读全文
0 0
- POJ 1986 Distance Queries(LCA Tarjan离线算法)
- poj 1986 Distance Queries(LCA离线Tarjan算法)
- POJ 1986 Distance Queries LCA离线算法tarjan
- poj 1968 Distance Queries LCA Tarjan 离线算法
- POJ 1986 Distance Queries(离线tarjan-LCA)
- POJ 1986 Distance Queries (Tarjan-LCA算法)(带权值)
- POJ---1986-Distance Queries(LCA-Tarjan)
- POJ 1986Distance Queries tarjan求LCA
- 【POJ】 1986 Distance Queries 离线LCA
- POJ 1986 Distance Queries(离线LCA)
- POJ - 1986 Distance Queries(LCA离线)
- poj 1986 Distance Queries 离线LCA
- (poj 1986 Distance Queries)<LCA—tarjan>
- POJ 题目1986 Distance Queries(LCA 离线)
- POJ 1986 Distance Queries(LCA在线和离线)
- POJ 1986 Distance Queries [LCA]
- poj 1986 Distance Queries LCA
- POJ 1986 Distance Queries(LCA)
- linux命令wc解读
- (转载)__declspec(dllimport)的作…
- C++的dllexport和dllimport …
- 类对象作为函数参数
- CvMat, Mat, IplImage,c…
- POJ 1986 Distance Queries LCA离线算法tarjan
- UDP局域网内广播消息(转载)
- MessageBox用C/C++实现一个提示信…
- magento2 拿到sql后,导入发现错误:Constraint "quote_to_store" references table that does not exist.
- (转载)opencv+GPU
- 三维重建——Structure from motion
- wchar_t与char转换(转载)
- 如何更改VS2010的[默认开发语言]默…
- hyperledger fabric0.6 结构分析(三)