poj 1986 Distance Queries(LCA求两点距离模版)
来源:互联网 发布:怎么找淘宝内部优惠券 编辑:程序博客网 时间:2024/06/01 07:57
Distance Queries
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 7673 Accepted: 2702Case 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.
题意:给出一个无向图,给出若干询问,每个询问求两点距离。
思路:tarjan连线算法求lca
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <map>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll long longusing namespace std;const int maxn=40005;struct node{ int v,w,next;} edge[maxn*2];struct node1{ int v,next,id;} que[maxn];int fa[maxn],G[maxn],Q[maxn],id[maxn],ans[maxn],dis[maxn];bool vis[maxn];int n,m,q,num,numq,cnt;void init(){ memset(G,-1,sizeof(G)); memset(Q,-1,sizeof(Q)); memset(vis,false,sizeof(vis)); num=numq=0;}void add(int u,int v,int w){ edge[num].v=v; edge[num].w=w; edge[num].next=G[u]; G[u]=num++;}void addq(int u,int v,int id){ que[numq].v=v; que[numq].next=Q[u]; que[numq].id=id; Q[u]=numq++;}int find_set(int x){ return x==fa[x]?x:fa[x]=find_set(fa[x]);}void Union(int a,int b){ int ra=find_set(a); int rb=find_set(b); if(ra!=rb) fa[rb]=ra;}void input(){ char s[3]; int a,b,c; while(m--) { scanf("%d%d%d%s",&a,&b,&c,s); add(a,b,c); add(b,a,c); } scanf("%d",&q); for(int i=1; i<=q; i++) { scanf("%d%d",&a,&b); addq(a,b,i); addq(b,a,i); }}void dfs(int u){ id[u]=cnt; fa[u]=u; vis[u]=true; for(int i=G[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(!vis[v]) { dis[v]=dis[u]+edge[i].w; dfs(v); Union(u,v); } } for(int i=Q[u]; i!=-1; i=que[i].next) { int v=que[i].v; if(vis[v]&&id[u]==id[v]) ans[que[i].id]=dis[u]+dis[v]-2*dis[find_set(v)]; }}void solve(){ memset(vis,false,sizeof(vis)); memset(id,0,sizeof(id)); for(int i=1;i<=n;i++) if(!vis[i]) { dis[i]=0; dfs(i); cnt++; } for(int i=1;i<=q;i++) printf("%d\n",ans[i]);}int main(){ while(~scanf("%d%d",&n,&m)) { init(); input(); solve(); } return 0;}
- poj 1986 Distance Queries(LCA求两点距离模版)
- POJ 1986 Distance Queries(查询两点距离,LCA)
- POJ 1986 Distance Queries LCA树上两点的距离
- POJ 1986Distance Queries tarjan求LCA
- poj 1986 Distance Queries 【LCA转RMQ 裸题】【求两点最短距离】
- Poj 1986 Distance Queries (LCA 树上两点间最短距离)
- POJ1986:Distance Queries(LCA求距离 + Tarjan)
- POJ 1986Distance Queries(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
- [POJ 1986] Distance Queries (LCA)
- Distance Queries - POJ 1986 LCA
- poj 1986 Distance Queries(LCA)
- POJ 1986 Distance Queries【LCA】
- Poj 1986 Distance Queries【LCA】
- default constructor
- spring中的scope
- poj 题目3468 A Simple Problem with Integers (线段树)
- XML
- C语言中的按位运算
- poj 1986 Distance Queries(LCA求两点距离模版)
- 该用哪一个消息队列呢?
- 剑指offer-day.3
- 如何统计servlet一次访问对带宽流量的占用
- MDK编译错误:C4056E type of input file 'xxx' unknown
- android获取图库图片并返回
- 1008
- CSS实例(五):简洁的栏目实现方式
- CF 294E Shaass the Great【Tree】