*【HDU - 2586】How far away ? (LCA模板题,倍增)

题干:

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 32 2
1 2 100
1 2
2 1

Sample Output

10
25
100
100

解题报告:

   没想到ccf的时候刚学的lca,,第二周实验室的题就用到了2333。。Tarjan的方法也学了一些抽空写了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 5e4 + 5;
bool vis[MAX];
int dep[MAX],fa[MAX][33],cost[MAX][33];int n,m;
vector<int> vv[MAX];
vector<int> ww[MAX];
//int f[MAX][30],fa[MAX];
//void dfs(int cur) {
//	vis[cur] = 1;
//	int up = vv[cur].size();
//	for(int i = 0; i<up; i++) {
//		int v = vv[cur][i];
//		if(vis[v]) continue;
//		fa[v] = cur;
//		dep[v] = dep[cur]+1;
//		dfs(v);
//	}
//}
//void bz() {
//	for(int i = 1; i<=n; i++) f[i][0] = fa[i];
//	for(int j = 1; j<=30; j++) {
//		for(int i = 1; i<=n; i++) {
//			f[i][j] = f[f[i][j-1]][j-1];
//		}
//	}
//}
void dfs(int cur, int rt) {fa[cur][0] = rt;dep[cur] = dep[rt] + 1;for(int i = 1; i < 31; ++i) {fa[cur][i] = fa[fa[cur][i - 1]][i - 1];cost[cur][i] = cost[fa[cur][i - 1]][i - 1] + cost[cur][i - 1];}int sz = vv[cur].size();for (int i = 0; i < sz; ++i) {if (vv[cur][i] == rt) continue;cost[vv[cur][i]][0] = ww[cur][i];dfs(vv[cur][i], cur);}
}
int lca(int u,int v) {if(dep[u] < dep[v]) swap(u,v);int res = 0;int dc = dep[u]-dep[v];for(int i = 0; i<=30; i++) {if((1<<i) & dc) res += cost[u][i],u=fa[u][i];}
//  for (int j = 0; dc; ++j, dc >>= 1)
//    if (dc & 1) res += cost[u][j], u = fa[u][j];if(u == v) return res;for(int i = 30; i>=0 && u!=v; i--) {if(fa[u][i] != fa[v][i]) {res += cost[v][i] + cost[u][i]; u=fa[u][i];v=fa[v][i];}}res += cost[u][0] + cost[v][0];	u=fa[u][0];return res;
}//int lca(int x, int y) {
//  if (dep[x] > dep[y]) swap(x, y);
//  int tmp = dep[y] - dep[x], ans = 0;
//  for (int j = 0; tmp; ++j, tmp >>= 1)
//    if (tmp & 1) ans += cost[y][j], y = fa[y][j];
//  if (y == x) return ans;
//  for (int j = 30; j >= 0 && y != x; --j) {
//    if (fa[x][j] != fa[y][j]) {
//      ans += cost[x][j] + cost[y][j];
//      x = fa[x][j];
//      y = fa[y][j];
//    }
//  }
//  ans += cost[x][0] + cost[y][0];
//  return ans;
//}
int main()
{int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);for(int i = 1; i<=n; i++) vv[i].clear(),ww[i].clear();for(int a,b,c,i = 1; i<=n-1; i++) {scanf("%d%d%d",&a,&b,&c);vv[a].pb(b);vv[b].pb(a);ww[a].pb(c);ww[b].pb(c);}dfs(1,-1);while(m--) {int a,b;scanf("%d%d",&a,&b);printf("%d\n",lca(a,b));}}return 0 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440987.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android volley 上传图片 和参数,Android使用Volley上传文件

一个项目中用到的使用Volley上传头像文件的例子/*** Created by wangshihui on 2015/11/30.* 上传文件* url&#xff1a;.....method&#xff1a;post参数&#xff1a;file接口给的参数&#xff1a;file 就是表单的key&#xff0c;传给mFilePartName;这是个测试类&#xff0c;…

【HDU - 4056】Draw a Mess (并查集 or 线段树)

题干&#xff1a; Its graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color. When teacher come to see what happened, without getting angry, he was surprised by the talented achiev…

android 按钮按下缩放,android捏缩放

我TextView使用本教程为我实现了一个缩放缩放。结果代码是这样的&#xff1a;private GestureDetector gestureDetector;private View.OnTouchListener gestureListener;并在onCreate()中&#xff1a;// Zoom handlersgestureDetector new GestureDetector(new MyGestureDetec…

【CodeForces - 520B】Two Buttons (bfs或dp或时光倒流,trick)

题干&#xff1a; Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After click…

android代码旋转屏幕,Android Activity源码分析--windowmanager屏幕旋转研究

注意&#xff1a;鄙人看的是6.0的代码Activity里面还是调用了WindowManager来显示界面。在activity的738行&#xff0c;有这几行代码private Window mWindow;private WindowManager mWindowManager;/*package*/ View mDecor null; //这就是activity的主view&#xff0c;我也不…

android surfaceflinger 代码,android surfaceflinger测试程序

frameworks/base/libs/surfaceflinger/tests/resize/resize.cpp 是个好地方。 但是我的测试应用程序版本( 来自供应商的Eclair ) 过时了&#xff0c;有些 Surface API已经转移到 SurfaceControl&#xff0c;你必须&#xff1a;SurfaceComposerClient::createSurface() > Sur…

【牛客 - 315B】 勇气获得机(二叉树性质,思维,知识点,tricks)

题干&#xff1a; 妞妞听说Nowcoder Girl女生编程挑战赛要开始了, 但是她没有足够的勇气报名参加, 牛牛为了帮助妞妞,给她准备一台勇气获得机。初始的时候妞妞的勇气值是0, 勇气获得机有两个按钮: 1、N按钮: 如果当期拥有的勇气值为x, 按下之后勇气值将变为2*x1&#xff0c…

【CodeForces - 357C 】Knight Tournament(并查集 或 STLset)

题干&#xff1a; Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event. As for you, youre just a simple peasant. …

qt android wifi,QtScrcpy: Android实时投屏软件,此应用程序提供USB(或通过TCP/IP)连接的Android设备的显示和控制。它不需要任何root访问权限...

QtScrcpyQtScrcpy可以通过USB(或通过TCP/IP)连接Android设备&#xff0c;并进行显示和控制。不需要root权限。单个应用程序最多支持16个安卓设备同时连接。同时支持GNU/Linux&#xff0c;Windows和MacOS三大主流桌面平台它专注于:精致 (仅显示设备屏幕)性能 (30~60fps)质量 (19…

android 添加so,Android studio 中添加 .so 文件

场景&#xff1a;Android studio 编译我的项目(项目中有运用的jni)&#xff0c;编译没有报错&#xff0c;正常的安装到我的机器上&#xff0c;可是运行的时候就报错&#xff0c;没有找到*.so文件...可是明明在libs&#xff0c;目录下有加相关的文件&#xff1f;参考网上大部分的…

【UVA - 10154 】Weights and Measures (贪心排序,dp,类似0-1背包,状态设定思维)

题干&#xff1a; The Problem Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles should be dispatched to form Yertles throne. Each of the five thousand, six hundred and seven turtles ordered by Yertle has …

投票抵制华为鸿蒙系统,网友投票华为十大技术:鸿蒙OS仅排第二!

作为国内消费电子巨头&#xff0c;华为的技术实力是有目共睹的&#xff0c;在过去的一年发布的许多黑科技让人眼前一亮&#xff0c;那么今日(17日)消息&#xff0c;华为终端今天表示&#xff0c;此前向粉丝们征集票选过去这一年里大家最关注的十大功能技术。最终&#xff0c;收…

【牛客 - 318L】彪神666(水题,半高精度,递推,trick)

题干&#xff1a; 在国外&#xff0c;666代表魔鬼&#xff0c;777代表上帝。 所以牛逼的彪神就非常不喜欢6这个数字。 有一天彪神突发奇想&#xff0c;&#xff0c;他想求一些书与6无关的数。 如果一个数能被6整除&#xff0c;或者它的十进制表示法中某位上的数字为6&…

平板android怎么玩电脑游戏,Android平板模拟家用主机游戏教程_小米 平板_平板电脑新闻-中关村在线...

一、NESoid看完了上一页Windows系统模拟器介绍的网友应该能得出一个经验&#xff0c;一般模拟器的名称都和其模拟的游戏主机名称比较类似&#xff0c;所以很多模拟器都可以通过其名称判断出它到底是模拟谁的。比如这款NESoid&#xff0c;看名字就知道是模拟NES主机&#xff0c;…

【牛客 - 297B】little w and Sum(水题,前缀和)

题干&#xff1a; 小w与tokitsukaze一起玩3ds上的小游戏&#xff0c;现在他们遇到了难关。 他们得到了一个数列&#xff0c;通关要求为这个数列的和为0&#xff0c;并且只有一次改变一个数的符号的机会(正数变成负数&#xff0c;负数变成正数)。 请问小w与tokitsukaze能否…

华为nova 7 se鸿蒙,荣耀v40和华为Nova7Pro哪个好-参数对比-更值得入手

荣耀v40已经发布&#xff0c;今天小编给大家带来荣耀v40和华为Nova7Pro参数详细分析&#xff0c;这两款手机有什么区别&#xff0c;哪一个更加值得入手呢&#xff0c;一起来看看吧一、参数对比迷你手机网荣耀v40​华为Nova7Pro手机外形屏幕尺寸6.72英寸6.57屏幕材质OLEDOLED刷新…

【HDU - 1102】Constructing Roads (最小生成树裸题模板)

题干&#xff1a; There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or the…

【牛客 - 318M】被打脸的潇洒哥(几何问题,水题,结论,知识点)

题干&#xff1a; 平面上有n个圆&#xff0c;求使这n个圆两两相交&#xff08;即每两个圆之间恰好有两个交点&#xff09;后最多能把平面划分成多少个区域。 输入描述: 一个正整数t&#xff0c;表示有t(1≤t≤100)组数据。 接下来t行&#xff0c;每行一个整数n(0≤n≤…

html中列表前面的序号带圆圈,js动态添加带圆圈序号列表方法的精讲

免费学习推荐&#xff1a;js视频教程1、先在body里面添加ul标签2.通过js获取到id等于list的标签定义一个空字符串用来连接增加的标签&#xff0c;并展示出来如图的js代码展示的是前三个颜色不同&#xff0c;余下的颜色相同的圆圈序号function autoAddList(){var oUl document.…

【HDU - 1757】A Simple Math Problem (矩阵快速幂)

题干&#xff1a; Lele now is thinking about a simple function f(x). If x < 10 f(x) x. If x > 10 f(x) a0 * f(x-1) a1 * f(x-2) a2 * f(x-3) …… a9 * f(x-10); And ai(0<i<9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two positive …