HDOJ2586 How far away ?
一道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 ?的更多相关文章
- 先说IEnumerable,我们每天用的foreach你真的懂它吗?
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 平台之大势何人能挡? 带着你的Net飞奔吧!
镇楼图: 跨平台系列: Linux基础 1.Linux基础学习 By dnt http://www.cnblogs.com/dunitian/p/4822807.html 环境配置 1.Hyper-v ...
- 谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- 百度推出新技术 MIP,网页加载更快,广告呢?
我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
- 复杂的 Hash 函数组合有意义吗?
很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...
- 你知道C#中的Lambda表达式的演化过程吗?
那得从很久很久以前说起了,记得那个时候... 懵懂的记得从前有个叫委托的东西是那么的高深难懂. 委托的使用 例一: 什么是委托? 个人理解:用来传递方法的类型.(用来传递数字的类型有int.float ...
- Linq表达式、Lambda表达式你更喜欢哪个?
什么是Linq表达式?什么是Lambda表达式? 如图: 由此可见Linq表达式和Lambda表达式并没有什么可比性. 那与Lambda表达式相关的整条语句称作什么呢?在微软并没有给出官方的命名,在& ...
- 搞个这样的APP要多久?
这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...
随机推荐
- CSS----学习2
CSS2属性 文本 1 水平对齐方式 text-align:left/right/center 也可以让img.input等有水平方向的对齐方式 2 垂直对齐方式 vertical-align:top ...
- Java NIO Files
Java NIO Files Files.exists() Files.createDirectory() Files.copy() Overwriting Existing Files Files. ...
- sqlserver自增主键
参考 https://www.cnblogs.com/michellexiaoqi/p/8031294.html 1.选中表: 2.右击鼠标,设计: 3.选中列(整数类 ...
- BufferedReader .BufferedWriter执行文本复制
/** * 需求:演示 BufferedReader 和 BufferedWriter 的使用,复制一个 java 文件 */ package cn.itcast.others.iostream; i ...
- jenkins搭建自动化部署平台(svn+tomcat重启)
原文链接:http://blog.csdn.net/zjs40/article/details/51818322 jenkins 自动化部署和集成代码的平台 可以将Git svn 上的代码 check ...
- 第四章 栈与队列(a)栈接口与实现
- Python+Selenium学习--鼠标事件
场景 前景讲解了鼠标的click()事件,而我们在实际的web产品测试中,有关鼠标的操作,不仅仅只有单击,有时候还包括右击,双击,拖动等操作,这些操作包含在ActionChains类中. Action ...
- python——线程相关
使用python的threading中的Thread 下面是两种基本的实现线程的方式: 第一种方式———— #coding=utf-8 """ thread的第一种声明及 ...
- easyui datagrid 表格不让选中(双层嵌套)
代码: function local(role,region,ip){ $("#roleList").datagrid({ // title:'服务器监控列表', height:( ...
- JAVA EXAM3 复习提纲
[Practice11_Zipcode_ArrayList] Zipcode class: //3 variables: zipcode, city, county, and compare by c ...