UESTC 1437
LCA模板题
随便找的倍增模板...
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
int to[maxn<<2],nxt[maxn<<2],cost[maxn<<2];
int head[maxn],tot;
int fa[maxn][20],dep[maxn];
void init(){
memset(head,-1,sizeof head);
memset(fa,0,sizeof fa);
tot=0;
}
void add(int u,int v,int w){
to[tot]=v;cost[tot]=w;
nxt[tot]=head[u];
head[u]=tot++;
}
void bfs(int root){
queue<int> que;
memset(dis,0,sizeof dis);
memset(fa,0,sizeof fa);
memset(dep,0,sizeof dep);
fa[root][0]=root;dep[root]=0;dis[root]=0;
que.push(root);
while(!que.empty()){
int u = que.front(); que.pop();
for(int i = 1; i < 20; i++) fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i = head[u]; ~i; i = nxt[i]){
int v=to[i],w=cost[i];
if(v==fa[u][0])continue;
dep[v]=dep[u]+1;dis[v]=dis[u]+w;fa[v][0]=u;
que.push(v);
}
}
}
int lca(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
for(int i = 0; i < 20; i++) if((dep[u]-dep[v])&(1<<i)) u=fa[u][i];
if(u==v)return u;
for(int i = 19; i >= 0; i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
int main(){
int n,m,u,v,w,x;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i = 1; i <= n-1; i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
bfs(1);
for(int i = 1; i <= m; i++){
scanf("%d%d",&u,&v);
x=lca(u,v);
printf("%d\n",dis[u]+dis[v]-2*dis[x]);
// cout<<"lca "<<x<<endl;
}
}
return 0;
}
UESTC 1437的更多相关文章
- ACM:UESTC - 649 括号配对问题 - stack
UESTC - 649 括号配对问题 Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu ...
- UESTC 1015 Lweb and pepper --前,后缀最值
题意: n种食物,每种含花椒的概率为Pi,现在已经选择了[L,R]这个区间(下标)的食物,要再选一个,使总的食物只有一种含花椒的概率最大,问选哪个最好,相同的选下标小的. 解法: 就不写解法了.此处有 ...
- UESTC 1852 Traveling Cellsperson
找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...
- UESTC 1851 Kings on a Chessboard
状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...
- UESTC 30 最短路,floyd,水
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...
- ural 1437. Gasoline Station
1437. Gasoline Station Time limit: 1.0 secondMemory limit: 64 MB Once a gasoline meter broke at a fi ...
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- 2015 CCPC D- Pick The Sticks(UESTC 1218) (01背包变形)
http://acm.uestc.edu.cn/#/problem/show/1218 既然二维dp表示不了,就加一维表示是否在边界放置,放置一个,两个.有一个trick就是如果只放一根,那么多长都可 ...
随机推荐
- hibernate学习笔记(1)基础配置与jar包
下载hibernate基础jar包,并解压hibernate-core-4.2.4.final 在myeclipse中添加hibernate的dtd支持: location为D:\学习\imooc-h ...
- ssh框架整合其他方式(没有hibernate核心配置文件)
- day36-hibernate检索和优化 09-Hibernate中的事务:事务处理
- JS/jQuery--iframe框架内外元素的操作(转)
JS/jQuery--iframe框架内外元素的操作 原创 2017年12月07日 14:23:09 标签: js / iframe 28 两个问题: 如何在父页面操作iframe框架内的元素? 如何 ...
- 恢复oracle的回收站的所有的表
使用sys as sysdba 进入到sqlplus的控制界面 sqlplus / as sysdba 执行相关的命令,自动生成一个脚本文件 spool d:/a.sql select 'flashb ...
- go语言的源码文件的分类及含义
Go源码文件:名称以.go为后缀,内容以Go语言代码组织的文件 多个Go源码文件是需要用代码包组织起来的 源码文件分为三类:命令源码文件.库源码文件(go语言程序) 测试源码文件(辅助源码文件) 命令 ...
- C++面向对象类的实例题目五
题目描述: 编写一个程序,采用一个类求n!,并输出5!的值. 程序代码: #include<iostream> using namespace std; class CFactorial ...
- IFC数据模式架构的四个概念层详解说明
IFC模型体系结构由四个层次构成,从下到上依次是 资源层(Resource Layer).核心层(Core Layer).交互层(Interoperability Layer).领域层(Domain ...
- 一个ButtonDemo的实现过程。
来自JDK API 1.6.0: Try this: Click the Launch button to run the Button Demo using Java™ Web Start (dow ...
- plsql developer 常用配置
执行语句时间太长,关闭 plsql developer 重启 F8 执行SQL 1 设置默认执行鼠标所在sql tools->preferences->sql window->win ...