-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56-Path on Tree.cpp
More file actions
39 lines (38 loc) · 886 Bytes
/
56-Path on Tree.cpp
File metadata and controls
39 lines (38 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
vector<vector<pair<int,int>>>v;
vector<int>dis;
void dfs(int x ,int p,int w)
{
dis[x]=w;
for(auto itr:v[x])
{
if(itr.fi!=p)
{
dfs(itr.fi,x,w+itr.se);
}
}
}
vector<int> pathOnTree (int n, int k, vector<vector<int>> edges, vector<vector<int>> queries)
{
dis.resize(n+1);
v=vector<vector<pair<int,int>>>(n+1,vector<pair<int,int>>());
int x,y,val;
for(int i=0;i<n-1;i++)
{
x=edges[i][0],y=edges[i][1],val=edges[i][2];
v[x].push_back({y,val});
v[y].push_back({x,val});
}
dfs(k,0,0);
vector<int>ans;
for(int i=0;i<queries.size();i++)
{
x=queries[i][0],y=queries[i][1];
val=dis[x]+dis[y];
ans.push_back(val);
}
return ans;
}