Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n,q,t1,t2,h,ans[100001];
  5. bool visited[100001]={false};
  6. vector<pair<int,int> > al[100001];
  7.  
  8. int r(int a, int b){
  9.     visited[a]=true;
  10.     ans[a]=b;
  11.     for(int i=0;i<al[a].size();i++){
  12.         if(!visited[al[a][i].first])r(al[a][i].first,b+al[a][i].second);
  13.     }
  14. }
  15.  
  16. int main(){
  17.     ios::sync_with_stdio(0);
  18.     cin >> n;
  19.     for(int i=1;i<n;i++){
  20.         cin >> t1 >> t2 >> h;
  21.         al[t1].push_back(make_pair(t2,h));
  22.         al[t2].push_back(make_pair(t1,-h));
  23.     }
  24.     r(1,1);
  25.     cin >> q;
  26.     while(q--){
  27.         cin >> t1 >> t2;
  28.         cout << ans[t2]-ans[t1]<<"\n";
  29.     }
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement