题面:[USACO08OCT]牧场散步Pasture Walking

题解:LCA模版题

代码:

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=,max_log=;
int N,Q,F[maxn+][max_log+],edge_head[maxn+],num_edge=,c,Dep[maxn+],u,v,w,A,B;
int Sig[maxn+];
struct Edge{
int to,nx,dis;
}edge[(maxn<<)+];
inline void Add_edge(int from,int to,int dis){
edge[++num_edge].nx=edge_head[from];
edge[num_edge].to=to;
edge[num_edge].dis=dis;
edge_head[from]=num_edge;
return;
}
inline void Dfs(int x,int fa){
Dep[x]=Dep[fa]+;
F[x][]=fa;
for(int i=;i<=max_log;i++)
F[x][i]=F[F[x][i-]][i-];
for(int i=edge_head[x];i;i=edge[i].nx){
int y=edge[i].to;
if(y==fa)continue;
Sig[y]=Sig[x]+edge[i].dis;
Dfs(y,x);
}
return;
}
inline int LCA(int x,int y){
if(Dep[x]<Dep[y]){
int t=x;x=y;y=t;
}
for(int i=max_log;i>=;i--){
int a;a=F[x][i];
if(Dep[a]>=Dep[y])x=a;
if(x==y)return x;
}
for(int i=max_log;i>=;i--){
int a,b;a=F[x][i];b=F[y][i];
if(a!=b){
x=a;y=b;
}
}
return F[x][];
}
int main(){
scanf("%d%d",&N,&Q);
for(int i=;i<N;i++){
scanf("%d%d%d",&u,&v,&w);
Add_edge(u,v,w);
Add_edge(v,u,w);
}
Dfs(,);
while(Q--){
scanf("%d%d",&A,&B);
c=LCA(A,B);
printf("%d\n",Sig[A]+Sig[B]-Sig[c]-Sig[c]);
}
return ;
}

By:AlenaNuna

LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking的更多相关文章

  1. Kruskal || BZOJ 1601: [Usaco2008 Oct]灌水 || Luogu P1550 [USACO08OCT]打井Watering Hole

    题面:P1550 [USACO08OCT]打井Watering Hole 题解:无 代码: #include<cstdio> #include<cstring> #includ ...

  2. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  3. BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )

    一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...

  4. luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...

  5. BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...

  6. BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题

    Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...

  7. BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs

    题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...

  8. BZOJ 1602 USACO2008 Oct 牧场行走

    翻翻吴大神的刷题记录翻到的... 乍一看是一个树链剖分吓瓜我...难不成吴大神14-10-28就会了树剖?orz... 再一看SB暴力都可过... 然后一看直接树上倍增码个就好了... 人生真是充满着 ...

  9. bzoj 1602: [Usaco2008 Oct]牧场行走【瞎搞】

    本来想爆手速写个树剖,然而快下课了就手残写了了个n方的短小-- 暴力把查询的两个点中深的一个跳上来,加上边权,然后一起跳加边权就行了 #include<iostream> #include ...

随机推荐

  1. InfluxDB服务器启动流程

    操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 源码路径: github.com/influxdata/infl ...

  2. bcrypt 加密

    关于 bcrypt:1.bcrypt是不可逆的加密算法,无法通过解密密文得到明文.2.bcrypt和其他对称或非对称加密方式不同的是,不是直接解密得到明文,也不是二次加密比较密文,而是把明文和存储的密 ...

  3. 使用Guava的RateLimiter完成简单的大流量限流

    限流的一般思路: 1.随机丢弃一定规则的用户(迅速过滤掉90%的用户): 2.MQ削峰(比如设一个MQ可以容纳的最大消息量,达到这个量后MQ给予reject): 3.业务逻辑层使用RateLimite ...

  4. kafka注册异常

    问题描述: kafka注册异常,提示brokers id已经被注册过 -- ::,] FATAL [Kafka Server ], Fatal error during KafkaServer sta ...

  5. JDBC告警系列(一)The server time zone value 'ÖÐ' is unrecognized or represents more than one time zone.

    一.现象 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents ...

  6. Ubuntu18.04提示wifi无法连接

    [时间:2018-07] [状态:Open] [关键词:Ubuntu 18.04,wifi,apt-get,mount] 0 现象描述及背景 最近需要一个Ubuntu的系统作为开发环境,随机下载了v1 ...

  7. IMAP 读取含有附件邮件超慢问题

    添加以下配置: Properties props = new Properties(); props.setProperty("mail.imap.partialfetch", & ...

  8. 分布式任务&分布式锁

    目前系统中存在批量审批.批量授权等各个操作,批量操作中可能因为处理机器.线程不同,造成刷新缓存丢失授权等信息,如批量审批同一用户权限多个权限申请后,流程平台并发的发送多个http请求到acl不同服务器 ...

  9. SAP 生产订单变更管理 OCM Order Changement Management

    SAP OCM Order Changement Management  一.目的 订单变更管理系统是当我们的订单(生产订单.计划订单.采购订单)已经存在的时候,其物料主数据或销售数据有变更时,我们可 ...

  10. 解决vscode无法安装golang相关插件的问题 - 即无法直连golang.org的问题

    喜欢挂vpn或者代理的请无视本文. 其实golang.org上的插件在github.com上都有镜像,直接 git clone https://github.com/golang/tools git ...