cdoj 92 Journey tarjan/lca 树上点对距离
Journey
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/problem/show/92
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≤105) and Q(1≤Q≤105), 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 xth city and the yth city, the first N−1 roads will form a tree structure, indicating the original roads, and the Nth 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 xth city to yth 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 ith line contains one integer indicating the time could saved in ith journey plan.
Sample Input
1
5 5
1 2 3
2 3 4
4 1 5
3 5 1
3 1 5
1 2
1 3
2 5
3 4
4 5
Sample Output
Case #1:
0
2
0
2
2
HINT
题意
给你一棵树,然后加了一条边,然后给Q次询问,问你这些点之间的最短距离缩短了多少
题解:
加了边之后,你走的方式就变成三种了,要么和原来一样,要么就是u-A-B-v,要么就是u-B-A-v这种
tarjan预处理一下距离跑一发就好了
代码:
//qscqesze
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200006
#define mod 1000000007
#define eps 1e-9
#define e exp(1.0)
#define PI acos(-1)
const double EP = 1E- ;
int Num;
//const int inf=0x7fffffff;
const ll inf=;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//***********************************************************
struct ndoe{
int v,w,next;
}ed[maxn*];
int dp[][maxn*],pos[maxn],dis[maxn],res[maxn],head[maxn],parent[maxn],vis[maxn];
int n,m,c,num,cnt,size;
void addedge(int u,int v,int w)
{
ed[num].v=v;
ed[num].w=w;
ed[num].next=head[u];
head[u]=num++;
}
int Find(int i)
{
if(i!=parent[i])
parent[i]=Find(parent[i]);
return parent[i];
}
void Union(int i,int j)
{
int x,y;
x=Find(i);
y=Find(j);
if(x!=y)
parent[x]=y;
}
int A,B,C,q;
void init()
{
int i,j,k;
n=read();q=read();
m=n-;
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
parent[i]=i;
cnt=size=num=;
while(m--)
{
scanf("%d%d%d",&i,&j,&k);
addedge(i,j,k);
addedge(j,i,k);
Union(i,j);
}
A=read(),B=read(),C=read();
}
void dfs(int u,int dist)
{
int i,j;
vis[u]=;
dis[u]=dist;
pos[u]=cnt;
res[size]=u;
dp[][cnt++]=size++;
for(i=head[u];i!=-;i=ed[i].next)
{
j=ed[i].v;
if(!vis[j])
{
dfs(j,dist+ed[i].w);
dp[][cnt++]=dp[][pos[u]];
}
}
}
void rmq()
{
int i,j,k;
for(i=;(<<i)<=n;i++)
for(j=n-;j>=;j--)
{
k=(<<(i-));
dp[i][j]=dp[i-][j];
if(k+j<n)
dp[i][j]=min(dp[i][j],dp[i-][j+k]);
}
}
int cal(int i,int j)
{
int k;
if(i<j)
{
i^=j;
j^=i;
i^=j;
}
k=;
while((<<k)<=(i-j+))
k++;
k--;
k=min(dp[k][j],dp[k][i-(<<k)+]);
return res[k];
}
int Dis(int u,int v)
{
int k = cal(pos[u],pos[v]);
return dis[u]+dis[v]-dis[k]*;
}
int tot = ;
void solve()
{
int i,j,k;
for(i=;i<=n;i++)
if(!vis[i])
dfs(i,);
n=n*-;
rmq();
printf("Case #%d:\n",tot);
for(int i=;i<=q;i++)
{
int u=read(),v=read();
int P = Dis(u,v);
int PP1 = Dis(u,A)+C+Dis(B,v);
int PP2 = Dis(u,B)+C+Dis(A,v);
PP1 = min(PP1,PP2);
printf("%d\n",max(P-PP1,));
}
}
int main()
{
int t=read();
while(t--)
{
tot++;
init();
solve();
}
return ;
}
cdoj 92 Journey tarjan/lca 树上点对距离的更多相关文章
- CDOJ 92 – Journey 【LCA】
[题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...
- CDOJ 92 Journey LCA乱搞
原题链接:http://acm.uestc.edu.cn/#/problem/show/92 题意: 给你一棵树,然后在树上连接一条边.现在有若干次询问,每次问你两个点(u,v)之间的距离在加那条边之 ...
- CDOJ 92 Journey(LCA&RMQ)
题目连接:http://acm.uestc.edu.cn/#/problem/show/92 题意:给定一棵树,最后给加一条边,给定Q次查询,每次查询加上最后一条边之后是否比不加这条边要近,如果近的话 ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
- 洛谷 P2783 有机化学之神偶尔会做作弊(Tarjan,LCA)
题目背景 LS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. “第1354题怎么做”<--手语 他问道 ...
- Tarjan+LCA【洛谷P2783】 有机化学之神偶尔会做作弊
[洛谷P2783] 有机化学之神偶尔会做作弊 题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 有一天他一边搓炉石一边监考,而你作为一个信息竞赛的大神也来凑热闹. 然而你的化竞基友却向你求助了. ...
- [BZOJ3307]:雨天的尾巴(LCA+树上差分+权值线段树)
题目传送门 题目描述: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式: 第一 ...
- LCA UESTC 92 Journey
题目传送门 题意:先给一棵树,然后有一条额外的边,问u走到v从现在最短的路走和原来不加边走的路节省了多少距离 分析:首先跑不加边的树的LCA,这样能求出任意两点的距离,那么现在x和y多连了一条边,如果 ...
- [luogu P3128][USACO15DEC]Max Flow [LCA][树上差分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
随机推荐
- Complete The Pattern #1
Complete The Pattern #1 Task: You have to write a function pattern which creates the following patte ...
- hdu4745Two Rabbits(dp)
链接 哎..比赛中一下想到了公共子序 之后思维就被局限了 一直在这附近徘徊 想着怎么优化 怎么预处理.. 观看了众多神牛的代码 ..以前觉得自己能写出个记忆化的最长回文长度 还挺高兴的...现在觉得好 ...
- bzoj1001
平面图求最小割: 其实看bzoj1001一开始着实把我怔住了 AC的人暴多,可自己完全没思路 后来看了某大牛的ppt,才会做 一个月前做这题的吧,今天来简单回忆一下: 首先是欧拉公式 如果一个连通的平 ...
- 解决IIS6.0不能下载EXE文件之妙方!
2003系统 IIS6建站 有EXE文件下载 但老是不让下载.当我把站删除再建后可以下载,但过一会又不让下载了.另,MIME里有EXE这项!请问这是为什么?应如何处理? 答案: win2003服务器允 ...
- Centos 下Nginx 自启动脚本
#!/bin/bash #ckconfig: NGINX_PATH=/web/container/nginx- NGINX_COMMAND=$NGINX_PATH/sbin/nginx NGINX_P ...
- (5)jvm垃圾回收器相关垃圾回收算法
引用计数法[原理]--->引用计数器是经典的也是最古老的垃圾收集防范.--->实现原理:对于对象A,只要有任何一个对象引用A,则计数器加1.当引用失效时,计数器减1.只要对象A的计数器值为 ...
- Using SharePoint 2010 dialogs
转:http://www.techbubbles.com/sharepoint/using-sharepoint-2010-dialogs/ SharePoint 2010 dialogs are J ...
- Integrating JavaScript into Native Applications
JavaScriptCore 简介 iOS7 中新加入的 JavaScriptCore.framework 可能被大多数开发人员所忽略,但是如果你之前就在项目中用过自己编译JavaScriptCore ...
- htmlcss笔记--a
a标签 1.下载:href里面放一个文件或者压缩包时,会下载: 2.锚点:跳转到锚点: href="#id" 跳转到的模块添加一个id,点击id就会跳转到该模块. html标签: ...
- Spring MVC @ModelAttribute
1.@ModelAttribute注释void返回值的方法 @Controller public class HelloModelController { @ModelAttribute public ...