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. AngularJs多重视图和路由的使用

    使用AngularJs来做多重视图和路由是在方便不过了,在开发过程中,都有许多的页面,而这些页面都有相同的部分,比如页面的头部和尾部通常都是一样的,变化的都是主体部分,还有就是一些后端管理的一些项目, ...

  2. 新手安装Oracle后的一些问题

      1.安装数据库服务端后,桌面上有个 打开之后点击一些按钮会叫你登录,用户名是SYSTEM,密码是安装的时候设置的密码(我设置的密码是root). . 2.在操作系统"运行"上输 ...

  3. 状压dp找寻环的个数 Codeforces Beta Round #11 D

    http://codeforces.com/problemset/problem/11/D 题目大意:给你n个点,m条边,找该图中有几个换 思路:定义dp[i][j]表示i是圈的集合,j表示该集合的终 ...

  4. Oracle10 多行和并

    SELECT fspc, wmsys.wm_concat (jsrzmc) as jsr FROM dxjsrxx GROUP BY fspc select fspc, LISTAGG(jsrzmc, ...

  5. jquery给html元素添加内容

    append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容 实例 $(& ...

  6. javascript IP验证

    //IP验证function isIP(strIP){try{if(strIP.length<7){return false;}var re=/^(\d+)\.(\d+)\.(\d+)\.(\d ...

  7. 一篇非常适合git入门的文章

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579 ...

  8. 小蚂蚁搬家<贪心>

    题意: 由于预知未来可能会下雨,所以小蚂蚁决定搬家.它需要将它的所有物品都搬到新家,新家的体积为V,小蚂蚁有N件物品需要搬,每件物品的体积为Ai,但他发现:每件物品需要新家剩余体积大于等于Bi才能使它 ...

  9. windows程序设计(二)

    MFC架构组成 1.CWinApp的派生类 2.必须在全局区定义一个派生类的对象 3.在CWinApp派生类内必须要有InitInstance虚函数的重写函数 在MFC软件工程以App类中的InitI ...

  10. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...