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. <marquee>,视频和音频的插入,正则表达式

    1.marquee 页面的自动滚动效果,不仅可以移动文字,也可以移动图片表格. <marquee></marquee>之间加内容即可. marquee的属性 1.滚动方向dir ...

  2. unlinking

    When a file name is deleted from the directory tree, the file name's connection to the inode number ...

  3. mysql存入中文乱码问题

    1. 查询编码 SHOW VARIABLES LIKE 'character_set_%' 2. 改数据库和单项 alter database tsdr character set utf8; set ...

  4. and的用法(&)

    经常看到jq的源码中各种&连接在一起赋值一个变量, 例一: var b = a&&a.getName 例二: var b = a&&a.getName||d 例 ...

  5. Augular JS里的各种ng

    Augular JS里的各种ng- 正文: 1.ng-disabled="一种状态:该状态下不可用"例如: %button.btn.btn-2(ng-disabled=" ...

  6. redis - java 基本操作

    import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; im ...

  7. mysql 不能插入中文

    三.#vim /etc/mysql/my.cnf .(5.5以前系统)在[client]下面加入 default-character-set=utf8 在[mysqld]下面加入default-cha ...

  8. Spring Boot 系列教程14-动态修改定时任务cron参数

    动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值 不能使用@Scheduled(cron = "${jobs.cron}")实现 DynamicSch ...

  9. LightOJ 1341 Aladdin and the Flying Carpet 算数基本定理

    题目大意:给出面积n,和最短边m,求能形成的矩形的个数(不能为正方形). 题目思路:根据算数基本定理有: 1.每个数n都能被分解为:n=p1^a1*p2^a2*^p3^a3……pn^an(p为素数); ...

  10. 空指针错误 java.lang.NullPointerException

    使用基本的JAVA数据类型,变量的值要么已经是默认值,如果没有对其正常赋值,程序便 不能通过编译,因此使用基本的JAVA数据类型(double,float,boolean,char,int,long) ...