一道LCA模板

原题链接

\(LCA\)模板题,不解释。

倍增版

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 4e4 + 10;
int fi[N], da[N << 1], di[N << 1], ne[N << 1], de[N], dis[N], f[N][17], gn, l;
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline void sw(int &x, int &y)
{
int z = x;
x = y;
y = z;
}
inline void add(int x, int y, int z)
{
di[++l] = y;
da[l] = z;
ne[l] = fi[x];
fi[x] = l;
}
void dfs(int x)
{
int i, y;
for (i = 1; i <= gn; i++)
f[x][i] = f[f[x][i - 1]][i - 1];
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (y != f[x][0])
{
de[y] = de[x] + 1;
dis[y] = dis[x] + da[i];
f[y][0] = x;
dfs(y);
}
}
}
int lca(int x, int y)
{
int i;
if (de[x] > de[y])
sw(x, y);
for (i = gn; ~i; i--)
if (de[f[y][i]] >= de[x])
y = f[y][i];
if (!(x^y))
return x;
for (i = gn; ~i; i--)
if (f[x][i] ^ f[y][i])
{
x = f[x][i];
y = f[y][i];
}
return f[x][0];
}
int main()
{
int i, n, m, x, y, z, t;
t = re();
while (t--)
{
n = re();
m = re();
memset(fi, 0, sizeof(fi));
l = 0;
gn = log2(n);
for (i = 1; i < n; i++)
{
x = re();
y = re();
z = re();
add(x, y, z);
add(y, x, z);
}
de[1] = 1;
dfs(1);
for (i = 1; i <= m; i++)
{
x = re();
y = re();
printf("%d\n", dis[x] + dis[y] - (dis[lca(x, y)] << 1));
}
}
return 0;
}

\(tarjan\)版

#include<cstdio>
using namespace std;
const int N = 4e4 + 10;
const int M = 210;
struct dd {
int y, nex, id;
};
dd q[M << 1];
int fi[N], di[N << 1], da[N << 1], ne[N << 1], dis[N], fa[N], an[M], fi_q[N], v[N], l, lq;
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c<'0' || c>'9'; c = getchar())
p |= c == '-';
for (; c >= '0'&&c <= '9'; c = getchar())
x = x * 10 + (c - '0');
return p ? -x : x;
}
inline void add(int x, int y, int z)
{
di[++l] = y;
da[l] = z;
ne[l] = fi[x];
fi[x] = l;
}
inline int fin(int x)
{
if (!(x^fa[x]))
return x;
return fa[x] = fin(fa[x]);
}
inline void add_qu(int x, int y, int z)
{
q[++lq].y = y;
q[lq].nex = fi_q[x];
q[lq].id = z;
fi_q[x] = lq;
}
void tarjan(int x)
{
int i, y;
v[x] = 1;
for (i = fi[x]; i; i = ne[i])
{
y = di[i];
if (!v[y])
{
dis[y] = dis[x] + da[i];
tarjan(y);
fa[y] = x;
}
}
for (i = fi_q[x]; i; i = q[i].nex)
{
y = q[i].y;
if (!(v[y] ^ 2))
an[q[i].id] = dis[x] + dis[y] - (dis[fin(y)] << 1);
}
v[x] = 2;
}
int main()
{
int i, n, m, x, y, z, t;
t = re();
while (t--)
{
n = re();
m = re();
for (i = 1; i <= n; i++)
{
fa[i] = i;
fi[i] = fi_q[i] = v[i] = 0;
}
l = lq = 0;
for (i = 1; i < n; i++)
{
x = re();
y = re();
z = re();
add(x, y, z);
add(y, x, z);
}
for (i = 1; i <= m; i++)
{
x = re();
y = re();
if (x^y)
an[i] = 0;
add_qu(x, y, i);
add_qu(y, x, i);
}
tarjan(1);
for (i = 1; i <= m; i++)
printf("%d\n", an[i]);
}
return 0;
}

HDOJ2586 How far away ?的更多相关文章

  1. 先说IEnumerable,我们每天用的foreach你真的懂它吗?

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  2. 平台之大势何人能挡? 带着你的Net飞奔吧!

    镇楼图: 跨平台系列: Linux基础 1.Linux基础学习 By dnt http://www.cnblogs.com/dunitian/p/4822807.html 环境配置 1.Hyper-v ...

  3. 谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  4. 百度推出新技术 MIP,网页加载更快,广告呢?

    我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...

  5. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

  6. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  7. 你知道C#中的Lambda表达式的演化过程吗?

    那得从很久很久以前说起了,记得那个时候... 懵懂的记得从前有个叫委托的东西是那么的高深难懂. 委托的使用 例一: 什么是委托? 个人理解:用来传递方法的类型.(用来传递数字的类型有int.float ...

  8. Linq表达式、Lambda表达式你更喜欢哪个?

    什么是Linq表达式?什么是Lambda表达式? 如图: 由此可见Linq表达式和Lambda表达式并没有什么可比性. 那与Lambda表达式相关的整条语句称作什么呢?在微软并没有给出官方的命名,在& ...

  9. 搞个这样的APP要多久?

    这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...

随机推荐

  1. react ES5 与ES6的写法

    ES5var React = require('react'); var ReactDOM = require('react-dom'); // 定义组件 var HelloMessage = Rea ...

  2. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  3. Snipaste截图

    Snipaste 是一个简单但强大的贴图工具,同时也可以执行截屏.标注等功能. 引自: https://blog.csdn.net/qq_36279445/article/details/702109 ...

  4. centos静态绑定IP地址

    Centos7 /etc/sysconfig/network-scripts/ifcfg-ens33

  5. JMeter学习(三十四)使用jmeter来发送json/gzip格式数据(转载)

    转载自 http://www.cnblogs.com/yangxia-test 一.使用jmeter来发送gzip数据 有时候我们需要模拟在客户端将数据压缩后, 发送(post)到服务器端. 通常这种 ...

  6. 算法之二分查找PK线性查找

    列表查找(线性查找) 本质就是列表的index() 顺序查找 也叫线性查找,从列表第一个元素开始,顺序进行搜索,知道找到元素或搜索到列表最后一个元素为止. 以下是示例代码: def line_sear ...

  7. leetcode 字符串类型题

    1,Vaild Palindrome bool isPalindrome(string& s) { transform(s.begin(), s.end(), s.begin(), tolow ...

  8. Mac IntelliJ IDEA 2017(java开发集成环境)附注册码和破解教程 v2017.3.5破解版

    原文:http://www.orsoon.com/Mac/155938.html 原文中含有软件下载地址 软件介绍 IntelliJ IDEA 2017 Mac激活版是Mac平台上的一款java开发集 ...

  9. 分布式处理框架MapReduce的深入简出

    1).MapReduce的概述 2).MapReduce 编程模型 3).MapReduce架构 4).MapReduce编程 Google MapReduce论文,论文发表于2004年 Hadoop ...

  10. phpStudy安装教程

    1.在phpStudy官网下载安装包(http://phpstudy.php.cn/)2.解压安装后,若提示没有“VC9.VC11.VC14运行库,注意是X86 32位”,则在phpStudy下载对应 ...