Description

Bob has traveled to byteland, he find the N cities in byteland formed a tree structure, a tree structure is very special structure, there is exactly one path connecting each pair of nodes, and a tree with N nodes has N - 1 edges.

As a traveler, Bob wants to journey between those N cities, and he know the time each road will cost. he advises the king of byteland building a new road to save time, and then, a new road was built. Now Bob has Q journey plan, give you the start city and destination city, please tell Bob how many time is saved by add a road if he always choose the shortest path. Note that if it's better not journey from the new roads,
the answer is 0.

Input

First line of the input is a single integer T(1 <= T <= 20), indicating there are T test cases.

For each test case, the first will line contain two integers N(2 <= N <= 10^5) and Q(1 <= Q <= 10^5), indicating the number of cities in byteland and the journey plans. Then N line followed, each line will contain three integer x, y(1 <= x,y <= N) and z(1 <= z <= 1000) indicating there is a road cost z time connect the x-th city and the y-th city, the first N - 1 roads will form a tree structure, indicating the original roads, and the N-th line is the road built after Bob advised the king. Then Q line followed, each line will contain two integer x and y(1 <= x,y <= N), indicating there is a journey plan from the x-th city to y-th city.

Output

For each case, you should first output "Case #t:" in a single line, where t indicating the case number between 1 and T, then Q lines followed, the i-th line contains one integer indicating the time could saved in i-th journey plan.

题目大意:给一棵树T,每条边都有一个权值,然后又一条新增边,多次询问:从点x到点y在T上走的最短距离,在加上那条新增边之后,最短距离可以减少多少。

思路:任意确定一个根root,DFS计算每个点到根的距离dis[],然后每两点间的最短距离为dis[x]+dis[y]-2*dis[LCA(x,y)]。若新加入一条边u--v,那么如果我们必须经过u--v,那么从x到y的最短距离就为dis(x,u)+dis(u,v)+dis(v,y)或dis(x,v)+dis(v,u)+dis(u,y)。这样在线处理答案就行。

PS:至于求LCA的方法可以参考2007年郭华阳的论文《RMQ&LCA问题》,RMQ可以用ST算法,至于那个O(n)的±1RMQ有空再写把……

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN = ;
const int MAXM = MAXN * ; int head[MAXN];
int next[MAXM], to[MAXM], cost[MAXM];
int ecnt, root; void init() {
ecnt = ;
memset(head, , sizeof(head));
} void addEdge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} int dis[MAXN]; void dfs(int f, int u, int di) {
dis[u] = di;
for(int p = head[u]; p; p = next[p]) {
if(to[p] == f) continue;
dfs(u, to[p], di + cost[p]);
}
} int RMQ[*MAXN], mm[*MAXN], best[][*MAXN]; void initMM() {
mm[] = -;
for(int i = ; i <= MAXN * - ; ++i)
mm[i] = ((i&(i-)) == ) ? mm[i-] + : mm[i-];
} void initRMQ(int n) {
int i, j, a, b;
for(i = ; i <= n; ++i) best[][i] = i;
for(i = ; i <= mm[n]; ++i) {
for(j = ; j <= n + - ( << i); ++j) {
a = best[i - ][j];
b = best[i - ][j + ( << (i - ))];
if(RMQ[a] < RMQ[b]) best[i][j] = a;
else best[i][j] = b;
}
}
} int askRMQ(int a,int b) {
int t;
t = mm[b - a + ]; b -= ( << t)-;
a = best[t][a]; b = best[t][b];
return RMQ[a] < RMQ[b] ? a : b;
} int dfs_clock, num[*MAXN], pos[MAXN];//LCA void dfs_LCA(int f, int u, int dep) {
pos[u] = ++dfs_clock;
RMQ[dfs_clock] = dep; num[dfs_clock] = u;
for(int p = head[u]; p; p = next[p]) {
if(to[p] == f) continue;
dfs_LCA(u, to[p], dep + );
++dfs_clock;
RMQ[dfs_clock] = dep; num[dfs_clock] = u;
}
} int LCA(int u, int v) {
if(pos[u] > pos[v]) swap(u, v);
return num[askRMQ(pos[u], pos[v])];
} void initLCA(int n) {
dfs_clock = ;
dfs_LCA(, root, );
initRMQ(dfs_clock);
} int mindis(int x, int y) {
return dis[x] + dis[y] - * dis[LCA(x, y)];
} int main() {
int T, n, Q;
int x, y, z, p;
int u, v;
initMM();
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
printf("Case #%d:\n", t);
scanf("%d%d", &n, &Q);
init();
for(int i = ; i < n - ; ++i) {
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, z);
}
scanf("%d%d%d", &x, &y, &z);
root = x;
dis[root] = ;
for(p = head[root]; p; p = next[p]) dfs(root, to[p], cost[p]);
initLCA(n);
while(Q--) {
scanf("%d%d", &u, &v);
int ans1 = mindis(u, v);
int ans2 = min(mindis(u, x) + mindis(y, v), mindis(u, y) + mindis(x, v)) + z;
if(ans1 > ans2) printf("%d\n", ans1 - ans2);
else printf("0\n");
}
}
}

  

UESTC 1717 Journey(DFS+LCA)(Sichuan State Programming Contest 2012)的更多相关文章

  1. 搜索分析(DFS、BFS、递归、记忆化搜索)

    搜索分析(DFS.BFS.递归.记忆化搜索) 1.线性查找 在数组a[]={0,1,2,3,4,5,6,7,8,9,10}中查找1这个元素. (1)普通搜索方法,一个循环从0到10搜索,这里略. (2 ...

  2. # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)

    「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...

  3. hdu----(2586)How far away ?(DFS/LCA/RMQ)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...

  5. hdu 3887 Counting Offspring(DFS序【非递归】+树状数组)

    题意: N个点形成一棵树.给出根结点P还有树结构的信息. 输出每个点的F[i].F[i]:以i为根的所有子结点中编号比i小的数的个数. 0<n<=10^5 思路: 方法一:直接DFS,进入 ...

  6. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  7. CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)

    Description A thief is running away! We can consider the city to N–. The tricky thief starts his esc ...

  8. poj2718 Smallest Difference(dfs+特判,还可以贪心更快)

    https://vjudge.net/problem/POJ-2718 其实不太理解为什么10超时了.. 这题似乎是有贪心优化的方法的,我下面直接暴力了.. 暴力之余要特判两个点:1.超时点就是n=1 ...

  9. 线段树(dfs序建树加区间更新和单点查询)

    题目链接:https://cn.vjudge.net/contest/66989#problem/J 记录一下这道折磨了我一天的题,.... 具体思路: 具体关系可通过dfs序建树,但是注意,在更新以 ...

随机推荐

  1. 【LightOJ 1081】Square Queries(二维RMQ降维)

    Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each c ...

  2. 前端的字符串时间如何自动转换为后端Java的Date属性,介绍springMVC中如何解决时间转换问题

    平常在开发过程中,前端选择时间一般都要使用时间选择插件,但是这种插件选出来的时间都是字符串类型,我们该怎么转换为后端的Date呢?/? 前端效果如下(笔者用的是layDate5.0插件): 修改前的后 ...

  3. 基于原生JS封装数组原型上的sort方法

    基于原生JS封装数组原型上的sort方法 最近学习了数组的原型上内置方法的封装,加强了用原生JS封装方法的能力,也进一步理解数组方法封装的过程,实现的功能.虽然没有深入底层,了解源码.以下解法都是基于 ...

  4. 『Python基础-12』各种推导式(列表推导式、字典推导式、集合推导式)

    # 『Python基础-12』各种推导式(列表推导式.字典推导式.集合推导式) 推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的 ...

  5. flask日志

    日志功能的实现 Python 自身提供了一个用于记录日志的标准库模块:logging. logging 模块 logging 模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统 log ...

  6. 小程序开发-13-小程序wxs的应用

    内容简介的换行 问题:因为微信的<text></text>标签能够转义\n,所以从服务器加载来的数据我们可以直接放到这个标签中,\n就会自己换行了.问题是服务器返回来的数据多了 ...

  7. 利用Python Counter快速计算出现次数topN的元素

    需要用Python写一段代码,给定一堆关键词,返回出现次数最多的n个关键字. 第一反应是采用一个dict,key存储关键词,value存储出现次数,如此一次遍历即可得出所有不同关键词的出现次数,而后排 ...

  8. BZOJ1083_繁忙的都市_KEY

    题目传送门 裸的最小生成树. code: /************************************************************** Problem: 1083 U ...

  9. [ES]Elasticsearch在windows下的安装

    1.环境 win7 64位 2.下载包环境 https://www.elastic.co/cn/downloads/elasticsearch 选择对应安装包 3.安装过程 解压安装包,例如我的,解压 ...

  10. 【机器学习笔记】自组织映射网络(SOM)

    什么是自组织映射? 一个特别有趣的无监督系统是基于竞争性学习,其中输出神经元之间竞争激活,结果是在任意时间只有一个神经元被激活.这个激活的神经元被称为胜者神经元(winner-takes-all ne ...