Distance in the Tree

Time limit: 1.0 second
Memory limit: 64 MB
A weighted tree is given. You must find the distance between two given nodes.

Input

The first line contains the number of nodes of the tree n (1 ≤ n ≤ 50000). The nodes are numbered from 0 to n – 1. Each of the next n – 1 lines contains three integers uvw, which correspond to an edge with weight w (0 ≤ w ≤ 1000) connecting nodes u and v. The next line contains the number of queries m (1 ≤ m ≤ 75000). In each of the next m lines there are two integers.

Output

For each query, output the distance between the nodes with the given numbers.

Sample

input output
3
1 0 1
2 0 1
3
0 1
0 2
1 2
1
1
2

分析:树上两点间距离,直接tarjan;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,p[maxn],ans[maxn],vis[maxn],q[maxn];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
vi query[maxn],a[maxn],len[maxn],id[maxn];
void dfs(int now,int pre,int gao)
{
if(pre!=-)q[now]=q[pre]+gao;
for(int i=;i<a[now].size();i++)
{
if(a[now][i]!=pre)
{
dfs(a[now][i],now,len[now][i]);
}
}
}
void dfs1(int now)
{
vis[now]=;
for(int i=;i<query[now].size();i++)
{
if(vis[query[now][i]])
{
int fa=find(query[now][i]);
ans[id[now][i]]=q[now]+q[query[now][i]]-*q[fa];
}
}
for(int x:a[now])
{
if(!vis[x])
{
dfs1(x);
p[x]=now;
}
}
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)p[i]=i;
rep(i,,n-)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
a[u].pb(v),a[v].pb(u);
len[u].pb(w),len[v].pb(w);
}
scanf("%d",&t);
rep(i,,t)
{
int a,b;
scanf("%d%d",&a,&b);
query[a].pb(b),query[b].pb(a);
id[a].pb(i),id[b].pb(i);
}
dfs(,-,-);
dfs1();
rep(i,,t)printf("%d\n",ans[i]);
//system("Pause");
return ;
}

ural1471 Distance in the Tree的更多相关文章

  1. Distance on the tree

    Distance on the tree https://nanti.jisuanke.com/t/38229 DSM(Data Structure Master) once learned abou ...

  2. 南昌网络赛J. Distance on the tree 树链剖分+主席树

    Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...

  3. 南昌网络赛J. Distance on the tree 树链剖分

    Distance on the tree 题目链接 https://nanti.jisuanke.com/t/38229 Describe DSM(Data Structure Master) onc ...

  4. 2019南昌邀请赛网络预选赛 J.Distance on the tree(树链剖分)

    传送门 题意: 给出一棵树,每条边都有权值: 给出 m 次询问,每次询问有三个参数 u,v,w ,求节点 u 与节点 v 之间权值 ≤ w 的路径个数: 题解: 昨天再打比赛的时候,中途,凯少和我说, ...

  5. Distance on the tree(数剖 + 主席树)

    题目链接:https://nanti.jisuanke.com/t/38229 题目大意:给你n个点,n-1条边,然后是m次询问,每一次询问给你u,v,w然后问你从u -> v 的路径上有多少边 ...

  6. 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)

    https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...

  7. 2019南昌邀请赛网络赛:J distance on the tree

    1000ms 262144K   DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...

  8. 2019年ICPC南昌网络赛 J. Distance on the tree 树链剖分+主席树

    边权转点权,每次遍历到下一个点,把走个这条边的权值加入主席树中即可. #include<iostream> #include<algorithm> #include<st ...

  9. 【树形dp】【CF161D】distance on a tree + 【P1352】没有上司的舞会

    T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了- ...

随机推荐

  1. Javascript教程

    Javascript教程 laiqun@msn.cn Contents 1. javascript嵌入方式 2. javascript语法 3. 数据类型 4. javascript变量 5. 字符串 ...

  2. 更改JFram标题栏图标

    方式一: package com.swing.test; import javax.swing.ImageIcon; import javax.swing.JFrame; public class a ...

  3. selection与range笔记

    selection对象代表当前激活选中区,通常是高亮的文本块 创建选中区: 1.拖拽文本 2.脚本创建 cerateRange() 获取selection对象 IE     document.sele ...

  4. ANT 操控 ORACLE数据库实践

    Ant 执行系统命令没有任何问题,这次实际系统命令中可以说遇到了两个问题,一个是启动服务的命令是含有空格的,第二个如何备份数据库可以自动加上日期. 首先,我们启动oracle数据库,操作有两个: 1. ...

  5. java方法:flush()

    flush本意是冲刷,这个方法大概取自它引申义冲马桶的意思,马桶有个池子,你往里面扔东西,会暂时保存在池子里,只有你放水冲下去,东西才会进入下水道. 同理很多流都有一个这样的池子,专业术语叫缓冲区,当 ...

  6. Webdriver中实现区域截图的方式以及如何截取frame中的图片

    import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOE ...

  7. html5利用websocket完成的推送功能

    利用websocket和java完成的消息推送功能,服务器用的是tomcat7.0,一些东西是自己琢磨的,也不知道恰不恰当,不恰当处,还请各位见谅,并指出. 程序简单来说,就是客户A可以发送消息给客户 ...

  8. iOS 6 Passbook 入门 1/2

    http://www.raywenderlich.com/zh-hans/23066/ios-6-passbook-%E5%85%A5%E9%97%A8-12 iOS 6 Passbook 入门 1/ ...

  9. 初探JavaScript魅力(五)

    JS简易日历    innerHTML <title>无标题文档</title> <script> var neirong=['一','二','三','四','五' ...

  10. 栈的java实现和栈的应用

    [例子和习题出自数据结构(严蔚敏版), 本人使用java进行实现.  转载请注明作者和出处,  如有谬误, 欢迎在评论中指正. ] 栈的实现 栈是一种先进后出的数据结构, 首先定义了栈需要实现的接口: ...