Guest User

Untitled

a guest
Jun 3rd, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.42 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<cstring>
  5. #include<math.h>
  6. using namespace std ;
  7.  
  8. #define f(i,s,n) for(int i=s;i<n;i++)
  9. #define X first
  10. #define Y second
  11. #define MAXN 300005
  12.  
  13. typedef long long ll ;
  14. #define pii pair<ll,ll>
  15. ll depth[MAXN], mcd[MAXN];
  16. ll dp[MAXN][20][2] ;
  17. vector<pii> al[MAXN] ;
  18.  
  19. ll dfs1(int node,int par)
  20. {// to create the depth and mcd arrays
  21.     ll mcdV = 0  ;
  22.     for(auto x:al[node])
  23.         if(x.X!=par)
  24.         {
  25.             dp[x.X][0][0] = node ;
  26.             dp[x.X][0][1] = x.Y ;
  27.             depth[x.X] = depth[node]+1 ;    
  28.             mcdV = max(mcdV,max(x.Y,x.Y+dfs1(x.X,node))) ;
  29.         }
  30.     return mcd[node] = mcdV ;
  31. }
  32. int main()
  33. {
  34.     ios::sync_with_stdio(0) ;
  35.     cin.tie(0) ;
  36.     int T;cin>>T;
  37.     while(T--)
  38.     {
  39.  
  40.         int n,q ;cin>>n>>q;
  41.         f(i,0,n+1) al[i].clear() ;
  42.         f(i,0,n-1)
  43.         {
  44.             int u,v,w; cin>>u>>v>>w ;
  45.             al[u].emplace_back(v,w) ;
  46.             al[v].emplace_back(u,w) ;
  47.         }
  48.         memset(dp,-1,sizeof(dp)) ;
  49.         memset(depth,0,sizeof(depth)) ;
  50.         memset(mcd,0,sizeof(mcd)) ;
  51.         dp[1][0][0] = 0 ;//parent of node 1 is 0
  52.         dp[1][0][1] = -1e18 ;//distance of 1's parent from it is very loooww
  53.         // depth[0] = -1 ;
  54.         depth[1] = 0 ;
  55.         dfs1(1,0) ;//dp[i][0][0], depth, mcd filled up now.
  56.         f(j,1,20) f(i,1,n+1) dp[i][j][0] = dp[dp[i][j-1][0]][j-1][0], dp[i][j][1] = dp[i][j-1][1]+dp[dp[i][j-1][0]][j-1][1] ;
  57.         //dp created for binary lifting.
  58.         f(i,0,q)
  59.         {
  60.             int u,v,x ;cin>>u>>v>>x ;
  61.             ll maxD = mcd[u]+x+mcd[v] ;
  62.             //check if the path to lca is greater than this.
  63.             if(depth[u]>depth[v]) swap(u,v) ;// u should  always be at the lower depth
  64.             int diff = depth[v]-depth[u] ;
  65.             long long dist = 0 ;
  66.             /*while(diff)//log(3e5)~20
  67.             {
  68.                 int jumpto = floor(log2(diff)) ;
  69.                 v = dp[v][jumpto][0] ;
  70.                 dist+=dp[v][jumpto][1] ;//<----this way of keeping distances might be wrong
  71.                 diff -= (1<<jumpto) ;
  72.             }
  73.             while(diff)
  74.             {
  75.                 int jumpto = diff&(-diff) ;
  76.                 dist+=dp[v][(int)log2(jumpto)][1] ;
  77.                 v = dp[v][(int)log2(jumpto)][0] ;
  78.                 diff-=jumpto ;
  79.             }*/
  80.             for(int j=19;j>=0;j--)
  81.             {
  82.                 if(diff&(1LL<<j))
  83.                 {
  84.                     dist+=dp[v][j][1] ;
  85.                     v = dp[v][j][0] ;
  86.                 }
  87.             }
  88.             //reached the common level
  89.             for(int j=19;j>=0;j--)//20
  90.             {
  91.                 int av = dp[v][j][0] ;//finding the 2^jth parent of v
  92.                 int uv = dp[u][j][0] ;// finding 2^jth parent of u
  93.                 if(av!=uv)//should be same when j is too big, memset(dp,-1,sizeof(dp)) should help here
  94.                 {// if the parents are the same, then we won't go up, otherwise we will.
  95.                     dist+=dp[v][j][1]+dp[u][j][1] ;//jumping simultaneously upwards
  96.                     v = av ;
  97.                     u = uv ;
  98.                 }
  99.             }
  100.             //parent of u and v is the lca
  101.             dist+=dp[v][0][1]+dp[u][0][1] ;
  102.             cout<<max(maxD,dist)<<"\n" ;
  103.         }
  104.     }
  105. }
  106. /*
  107. 1
  108. 7 3
  109. 1 2 1
  110. 1 3 -2
  111. 2 4 3
  112. 2 5 -4
  113. 5 7 5
  114. 3 6 6
  115. 2 3 1
  116. 5 4 2
  117. 5 6 0
  118.  */
Add Comment
Please, Sign In to add comment