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. ios 导航页面

    //  AppDelegate.m#import "AppDelegate.h"#import "ViewController.h" @interface Ap ...

  2. 安卓无法生成R文件原因

    原因个人总结出来: 清单文件报错,则无法生成R文件 gen和bin目录可以删除

  3. centos7配置ip

    vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 ONBOOT=yes 重启ip服务 systemctl restart network.service 开 ...

  4. external 里面文件的介绍

    搞了半天android,竟然对external下的库一无所知?不能容忍! 马上解决: android-mock:编译为java静态库.说明:Android Mock is a framework fo ...

  5. 覆盖问题<shui>

    题目链接 /* hang[maxn]标记每行是否可以被攻击,并计算前缀和 lie [maxn]标记每列是否可以被攻击,并计算前缀和 */ #include<cstdio> // #incl ...

  6. NSIndexPath 延伸

    转载自:http://my.oschina.net/u/2560887/blog/602095?fromerr=Dy4vj5Jd 这个类的实例描述了一个嵌套数组中特定节点的路径,一般叫做索引路径.1. ...

  7. HDU 1010 Temper of the bone(深搜+剪枝)

    Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  8. Nape实现坐标旋转角度回弹

    乒乓球以一个向量运动,碰到障碍后反弹以一个新的向量运动,如下图: 要实现回弹只需要求出向量v1,把向量v0取反,再旋转(a+b)度就可以得到向量v1. 向量取反: var v:vec2 = new V ...

  9. STM32F4xx FPU的设置

    原文:http://blog.csdn.net/dlutxie/article/details/7980389 浮点运算一直是定点CPU的难题,比如一个简单的1.1+1.1,定点CPU必须要按照IEE ...

  10. 用phpstudy搭建dedecms网站验证码出不来解决方案

    验证码图片不显示,这应该是很多站长朋友们最长遇到的一个问题,本地测试明明好好的,为什么传上空间或者服务器上验证码就无法显示了呢,春哥分析这可能是由于没有加载gd库扩展所引起的,那么怎么解决呢?由于引起 ...