hdu 2586 How far away ?

【三维重建】基于多视角图像的三维重建 基于多视角图像的三维重建》 阅读详情


How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15599    Accepted Submission(s): 5924


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
 

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


题意:给你一棵树,每条边都有权值,m次询问,每次问任意两点之间的距离。

思路:我是特意来补这道题的。。因为感觉这个算法挺实用的,好像很多时候都要用到。这是倍增LCA的裸题。思维类似RMQ,通过预处理出每个点的第2^n次方个父节点,从而达到logn查询任意两个节点的最近公共祖先。具体写法看代码吧。。。自己手写的留着当模板用。

#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<map>
#include<vector>
#include<vector>
#define maxn 40005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int M = 20;
struct node{
	int v, value, next;
}p[maxn << 1];
int len, head[maxn], dis[maxn], depth[maxn], n, m, father[maxn][25];
void addedge(int u, int v, int value){
	p[len].v = v;
	p[len].value = value;
	p[len].next = head[u];
	head[u] = len++;
}
void dfs(int x, int fa){
	father[x][0] = fa;
	for (int i = head[x]; ~i; i = p[i].next){
		if (p[i].v == fa)
			continue;
		dis[p[i].v] = dis[x] + p[i].value;
		depth[p[i].v] = depth[x] + 1;
		dfs(p[i].v, x);
	}
}
void presolve(){
	dis[1] = depth[1] = 0;
	dfs(1, 0);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j < M; j++)
			father[i][j] = father[father[i][j - 1]][j - 1];
}
int lca(int x, int y){
	if (depth[x] != depth[y]){
		if (depth[x] > depth[y])
			swap(x, y);
		int distant = depth[y] - depth[x];
		for (int i = 0; i < M; i++){
			if (distant&(1 << i))
				y = father[y][i];
		}
	}
	if (x == y)
		return x;
	for (int i = M; i >= 0; i--){
		if (father[x][i] != father[y][i]){
			x = father[x][i];
			y = father[y][i];
		}
	}
	return father[x][0];
}
int query(int x, int y){
	return dis[x] + dis[y] - (dis[lca(x, y)] << 1);
}
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		len = 0;
		memset(head, -1, sizeof(head));
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n - 1; i++){
			int u, v, value;
			scanf("%d%d%d", &u, &v, &value);
			addedge(u, v, value);
			addedge(v, u, value);
		}
		presolve();
		while (m--){
			int x, y;
			scanf("%d%d", &x, &y);
			printf("%d\n", query(x, y));
		}
	}
}


MediaPipeUnityPlugin(最新版)摇摆拳人脸识别 阅读详情

相关推荐

Linux密码破解(使用hydra工具和使用john暴力破解)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录一、使用hydra离线破解linux密码1.准备实验环境2.创建用户名字典3.hydra密码爆破二、复制shadow文件,使用john暴力破解1.实验环境1.复制linux的shadow文件2.john暴力破解 一、使用hydra离线破解linux密码 1.准备实验环境 ①攻击机:kali_linux 2021.2(ifconfig查询IP地址:192.168.119.132) ②靶机:Ubantu 16.04(ifconfig

weixin_46447549的博客 2万+

hdu2586 How far away? LCA

hdu2586 #include #include #include #include using namespace std; #define MAX 40000 struct edge{ int v,w; }; vector mp[MAX]; vector query[MAX]; bool flag[MAX]; int pre[MAX],father[MAX],path[MAX]

Time flies 1943

TwinCAT 3 实战指南:从系统配置到高级运动控制

本文详细介绍了TwinCAT 3工业自动化控制平台的实战应用,从系统配置、PLC编程到EtherCAT总线配置和高级运动控制。通过倍福(Beckhoff)的TwinCAT 3平台,工程师可以高效实现复杂的工业自动化任务,包括运动控制、PLC编程等。文章还提供了常见问题排查和性能优化技巧,帮助用户提升系统稳定性和实时性。

weixin_30831529的博客 304

hdu2586【How far away ?】

[tarjian离线求lca] hdu2586【How far away ?】 Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13605 Accepted Submission(s): 5101 【Problem Descript

Freddddd 523

ARM Cortex-M系列授权解析:从研发到量产的免费与付费之路

本文深入解析了ARM Cortex-M系列内核从研发到量产过程中的授权模式。研发阶段可利用ARM DesignStart等免费资源进行原型验证,但产品商业化量产则需支付授权费与版税。文章对比了采购商用MCU与自研芯片的成本差异,并探讨了RISC-V作为免费替代方案在IoT领域的应用前景与挑战,为开发者提供了清晰的选型指南。

weixin_29292431的博客 494

Python爬虫(一)京东商品价格及详情页抓取

这是一个以http://item.jd.com/2957726.html为例的单网页抓取案例 import urllib.request; #载入urllib.request,用于获取页面html源代码 from pandas import Series; #载入series包 from pandas import DataFrame; #载入dataframe包 from bs4 impo

Data Proc 1万+

HDU 2586 How far away ?

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6309    Accepted Submission(s): 2368 Problem Description There are n

凌风 3953

别再折腾虚拟机了!手把手教你用WSL2在Windows 11上安装Kali Linux(含汉化与图形界面)

本文详细介绍了如何在Windows 11上通过WSL2安装和配置Kali Linux,包括汉化与图形界面设置。通过WSL2,用户可以享受接近原生性能的Linux体验,同时保持与Windows系统的无缝集成,特别适合网络安全爱好者和开发者。文章还提供了常见问题解决和性能优化建议,帮助用户高效使用Kali工具集。

weixin_42525369的博客 322

HDU 2586 How far away?(LCA)

题目链接; HDU 2586 How far away? 题意: 和POJ 1986 Distance Queries一样的,包括数据范围。 分析: 这里建单向边就能过了。。。#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; typede

ramay7 487

第42篇FEDGLOSS算法BEYOND LOCAL SHARPNESS:COMMUNICATION-EFFICIENT GLOBALSHARPNESS-AWARE MINIMIZATIONFOR

:用上一轮伪梯度最小化锐度,获取与全局损失最相关的几何信息,引导全局优化趋向更平坦的最小值。效果对比分析:FEDGLOss。

weixin_74009895的博客 526

RFC-only 与 SAProuter 场景下,让 Claude 真正参与 SAP ABAP 开发

SAP ABAP 开发这几年正在出现一个很有意思的变化。过去我们说开发工具,脑子里大多是 SE80、SE38、SE24、SEGW、Eclipse ADT,再往后是 BAS、VS Code、abapGit、ATC、CI/CD。现在又多了一层,Claude Code、Claude Desktop 这类 AI 编程助手开始通过 MCP 进入开发现场,不只是写一段示例代码,而是能读系统里的 ABAP 对象、搜索包、改源码、跑检查、辅助重构。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1617

HDU 2586 How far away ?题解与分析

HDU 2586 How far away ? 【题目大意】:           一个村子里有n个房子,这n个房子用n-1条路连接起来,接下来有m次询问,每次询问两个房子a,b之间的距离是多少。 【分析】:          这是个求最近公共祖先的问题,用临接链表存下每条边和询问的信息,然后跑一遍Tarjan(算法详解见:http://blog.youkuaiyun.com/csyzcyj/arti

初学者 1405

《Zephyr OS 入门与实战》第15章:LoRaWAN 集成

【代码】《Zephyr OS 入门与实战》第15章:LoRaWAN 集成。

2301_79143213的博客 608

hdu 2586 How far away

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11699    Accepted Submission(s): 4300 Problem Description There are n ho

keshuqi的博客 394

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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 h...

Tizzi的博客 914

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

HDU2586 How far away ?【LCA+并查集】

How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 33479 Accepted Submission(s): 13542 Problem Description There are n houses in...

海岛Blog 636

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

HDU-2586 How far away ?(Tarjian_LCA)

HDU-258 How far away ? 题目 给一颗树,及其每条边权值,求多组两点最短路径, 分析 树上两点最短路径可以先把每个节点到跟节点的权值dis求出,设rt为u,v的LCA。 u,v最短路径 : dis[u]+dis[v]−2∗dis[rt]dis[u] + dis[v] - 2 * dis[rt]dis[u]+dis[v]−2∗dis[rt] 我用的离线Tarjian算法,所以要先...

Stormjing的博客 332

HDU 2586——How far away ?

Time limit 1000 ms Memory limit 32768 kB 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...

天际苍穹的博客 226

hdu 2586 How far away ?倍增LCA

hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组 因为 \(2^{16} > 40000\) 所以所以表示祖先的数组dp[][]第二维取到16即可 就这道题来说,与比较tarjan比较,稍快一点 代码:...

ddqb3235的博客 234

HDU2586 How far away? —— 倍增LCA

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16425    Accepted

A man can be destroyed, but not defeated. 471

hdu 2586 How far away ? DFS大水题

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2935    Accepted Submission(s): 1095 Problem Description There are n h

hnust_xiehonghao的专栏 2758

HDU 2586. How far away ?

链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意 多次询问,求树形图上两点间距离 思路一 树链剖分 将一个点与其拥有子树节点数目的节点相连,形成一条重链,一颗树最多有 lognlognlogn 条重链 当 xxx 和 yyy 不在一条重链上时,将所在链顶点深度大的那个点往上跳,直到在一条链上 时间复杂度:O((n+m)logn)O((n+m)logn)O((n+m)logn) 代码 #include<bits/stdc++.h> using

c4Lnn 的 优快云 博客 192

How far away ? HDU - 2586

How far away ? Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27977Accepted Submission(s): 11218 Problem Description There are n houses ...

weixin_30902675的博客 240

HDU 2586 How far away?

HDU 2586#pragma comment(linker,"/STACK:1024000000,1024000000") #include&lt;bits/stdc++.h&gt; using namespace std; vector&lt;int&gt;f[40010],flen[40010]; int n,m,dis[40010],dep[40010],fat[40010],p[4001...

Napoleon2004的博客 159

HDU2586(How far away)

How far away 题目传送门 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 luck

weixin_43808717的博客 780

HDU2586——How far away ?(LCA模板)

How far away ? 题意:给出一棵树,问两个节点x,y之间的最短距离是多少(边权值)。 思路:用根节点到x的距离+根节点到y的距离−多走的距离根节点到x的距离+根节点到y的距离-多走的距离根节点到x的距离+根节点到y的距离−多走的距离 其中多走的距离要用LCA解决。 LCALCALCALCALCALCALCA求解方法1.欧拉序+++线段树或RMQ欧拉序RMQ(线段树)2.Tarjan LCALCALCA LCALCALCA只是一个概念,它的是一棵树上的两个节点的最近公共祖先。当然,它也是一个节点。

weixin_45363113的博客 607
上一篇: Codeforces Round #411 (Div. 2)E. Ice cream coloring
下一篇: 倍增LCA模板
overload1997
博客等级 码龄11年 328粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值