Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int n,q,t1,t2,h,ans[100001];
- bool visited[100001]={false};
- vector<pair<int,int> > al[100001];
- int r(int a, int b){
- visited[a]=true;
- ans[a]=b;
- for(int i=0;i<al[a].size();i++){
- if(!visited[al[a][i].first])r(al[a][i].first,b+al[a][i].second);
- }
- }
- int main(){
- ios::sync_with_stdio(0);
- cin >> n;
- for(int i=1;i<n;i++){
- cin >> t1 >> t2 >> h;
- al[t1].push_back(make_pair(t2,h));
- al[t2].push_back(make_pair(t1,-h));
- }
- r(1,1);
- cin >> q;
- while(q--){
- cin >> t1 >> t2;
- cout << ans[t2]-ans[t1]<<"\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement