HDOJ 题目2586 How far away ?(LCA)

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7090    Accepted Submission(s): 2578

Problem Description

There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this
village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can‘t visit a place twice) between every two houses. Yout task is to answer all these curious people.

Input

First line is a single integer T(T<=10), indicating the number of test cases.

For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road
connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.

Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.

Output

For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.

Sample Input

2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

Sample Output

10
25
100
100

Source

ECJTU 2009 Spring Contest

Recommend

lcy   |   We have carefully selected several similar problems for you:  3486 2874 2888 3234 3461

ac代码

#include<stdio.h>
#include<string.h>
int vis[40040],head[40040],n,m,x[40040],y[40040],dis[40040],cnt,pre[40040],z[40040];
struct s
{
	int u,v,w,next;
}edge[40005*2];
void init()
{
	memset(vis,0,sizeof(vis));
	memset(head,-1,sizeof(head));
	memset(dis,0,sizeof(dis));
	cnt=0;
}
void add(int u,int v,int w)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int find(int x)
{
	if(x==pre[x])
		return x;
	return find(pre[x]);
}
void tarjan(int u)
{
	int i;
	pre[u]=u;
	vis[u]=1;
	for(i=1;i<=m;i++)
	{
		if(x[i]==u&&vis[y[i]])
			z[i]=find(y[i]);
		else
			if(y[i]==u&&vis[x[i]])
				z[i]=find(x[i]);
	}
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(!vis[v])
		{
			dis[v]=dis[u]+edge[i].w;
			tarjan(v);
			pre[v]=u;
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int i;
		init();
		for(i=0;i<n-1;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
			add(v,u,w);
		}
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&x[i],&y[i]);
		}
		tarjan(1);
		for(i=1;i<=m;i++)
		{
			printf("%d\n",dis[x[i]]+dis[y[i]]-2*dis[z[i]]);
		}
	}
}
时间: 2024-08-07 12:45:06

HDOJ 题目2586 How far away ?(LCA)的相关文章

HDOJ 2586 How far away? LCA

http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意: 给一棵树,多次查询点到点距离. 分析: d[x][y] = d[x][root] + d[y][root] - 2 * d[lca(x,y)][root],所以求lca即可.因为tarjan是在dfs的过程中求lca,顺便就把点到根的距离也求了. 敲了代码才发现,讲解的伪代码都是先递归子节点,染色,再回答询问的顺序,但是平时写的时候一般是建了双向边,先递归子节点再染色会导致环的情况从而死循环.所

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

HDOJ 题目4738 Caocao&#39;s Bridges(双联通,求桥)

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1752    Accepted Submission(s): 642 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

HDOJ题目3729 I&#39;m Telling the Truth(二分图)

I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1629    Accepted Submission(s): 805 Problem Description After this year's college-entrance exam, the teacher did a survey i

HDOJ 题目4349 Xiao Ming&#39;s Hope(找规律)

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 1015 Problem Description Xiao Ming likes counting numbers very much, especially he is fond of co

HDOJ 题目分类

HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:    用STL中的next_permutation()1029:1032:1037:1039:1040:1056:1064:1065:1076:    闰年 1084:1085:1089,1090,1091,1092,1093,1094, 1095, 1096:全是A+B1108:1157:1196:1

hihoCoder挑战赛11.题目4 : 高等理论计算机科学(LCA)

clj在某场hihoCoder比赛中的一道题,表示clj的数学题实在6,这道图论貌似还算可以... 题目链接:http://hihocoder.com/problemset/problem/1167 由于是中文题目,题意不再赘述. 对于任意两条小精灵的活动路径a和b,二者相交的判断条件为b的两个端点的LCA在a的路径上:那么我们可以首先将每个活动路径端点的LCA离线预处理出来,对每个节点LCA值+1. 然后以某个节点(我选择的是节点1)为根进行深搜,算出一条从节点1到节点x的LCA值和,那么任意

HDOJ 题目5097 Page Rank(矩阵运算,模拟)

Page Rank Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 280    Accepted Submission(s): 75 Problem Description Evaluation and rank of web pages is a hot topic for many internet companies and

HDOJ 题目2475 Box(link cut tree去点找祖先)

Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2374    Accepted Submission(s): 718 Problem Description There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes