POJ 题目1986 Distance Queries(LCA 离线)

Distance Queries

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 10142   Accepted: 3575
Case 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.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart.

Source

USACO 2004 February

求树上两个点最近距离

ac代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct s
{
	int u,v,w,next;
}edge[100100];
struct que
{
	int u,v,num,next;
}q[100010];
int head1[100100],head2[100010],cnt1,cnt2,dis[100100],pre[100100],n,m,qq,vis[100100],ans[100100	];
int find(int x)
{
	if(x==pre[x])
		return x;
	return find(pre[x]);
}
void init()
{
	memset(head1,-1,sizeof(head1));
	memset(head2,-1,sizeof(head2));
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
	cnt1=cnt2=0;
}
void add1(int u,int v,int w)
{
	edge[cnt1].u=u;
	edge[cnt1].v=v;
	edge[cnt1].w=w;
	edge[cnt1].next=head1[u];
	head1[u]=cnt1++;
}
void add2(int u,int v,int i)
{
	q[cnt2].u=u;
	q[cnt2].v=v;
	q[cnt2].num=i;
	q[cnt2].next=head2[u];
	head2[u]=cnt2++;
}
void tarjan(int u,int len)
{
	int i,v;
	pre[u]=u;
	dis[u]=len;
	vis[u]=1;
	for(i=head2[u];i!=-1;i=q[i].next)
	{
		if(vis[q[i].v])
			ans[q[i].num]=dis[u]+dis[q[i].v]-2*dis[find(q[i].v)];
	}
	for(i=head1[u];i!=-1;i=edge[i].next)
	{
		v=edge[i].v;
		if(!vis[v])
		{
			tarjan(v,len+edge[i].w);
			pre[v]=u;
		}
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		while(m--)
		{
			int a,b,c;
			char str[2];
			scanf("%d%d%d%s",&a,&b,&c,str);
			add1(a,b,c);
			add1(b,a,c);
		}
		scanf("%d",&qq);
		int i;
		for(i=1;i<=qq;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			add2(a,b,i);
			add2(b,a,i);
		}
		tarjan(1,0);
	//	int i;
		for(i=1;i<=qq;i++)
		{
			printf("%d\n",ans[i]);
		}
	}
}
时间: 2024-12-28 19:57:11

POJ 题目1986 Distance Queries(LCA 离线)的相关文章

POJ 1986 Distance Queries LCA树上两点的距离

题目来源:POJ 1986 Distance Queries 题意:给你一颗树 q次询问 每次询问你两点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求最近公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn =

POJ 1986 Distance Queries LCA两点距离树

标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求近期公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int max

poj 1986 Distance Queries LCA

题目链接:http://poj.org/problem?id=1986 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

POJ——T 1986 Distance Queries

http://poj.org/problem?id=1986 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14383   Accepted: 5063 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their lei

POJ - 1986 Distance Queries(离线Tarjan算法)

1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - 2 * dis[lca(u, v)] 3. /* 离线算法,LCATarjan 复杂度O(n+Q); */ #include<iostream> #include<stdio.h> #include<string.h> using namespace std; const

POJ 1986 Distance Queries 【输入YY &amp;&amp; LCA(Tarjan离线)】

任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 16648   Accepted: 5817 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much t

POJ1986 Distance Queries (LCA)

传送门: http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K       Case 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 lifesty

poj 1986 Distance Queries 带权lca 模版题

Distance Queries 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

poj-1986 Distance Queries(lca+ST+dfs)

题目链接: Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 11531   Accepted: 4068 Case 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 l