Guest User

Untitled

a guest
May 26th, 2018
1,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std ;
  4. typedef long long ll ;
  5. #define F first
  6. #define S second
  7. const int MX = 100010 , MXLOG = 30 ;
  8. ll INF = 1e17 ;
  9. vector< pair< int , ll > > adj[MX] ;
  10. vector< int > cycle ;
  11. int vis[MX] , f , clr , cyc_len , head[MX] , par[MX][32] , dep[MX] ;
  12. ll cost[MX] , pref[MX] ;
  13. stack< int > st ;
  14. map< int , int > num ;
  15. void init ()
  16. {
  17.     for( int i = 0 ; i < MX ; ++i )adj[i].clear() ;
  18.     for( int i = 0 ; i < MX ; ++i )cost[i] = INF ;
  19.     cycle.clear() ;
  20.     num.clear() ;
  21.     memset( head , 0 , sizeof head ) ;
  22.     memset( par , -1 , sizeof par ) ;
  23.     memset( pref , 0 , sizeof pref ) ;
  24.     memset( dep , 0 , sizeof dep ) ;
  25.     while( !st.empty() )st.pop() ;
  26.     cyc_len = 0 ;
  27.     f = 0 ;
  28.     clr++ ;
  29. }
  30.  
  31. void dfs( int nd , int p = -1 )
  32. {
  33.     if( f ) return ;
  34.     vis[nd] = clr ;
  35.     st.push(nd) ;
  36.     for( auto it : adj[nd] )
  37.     {
  38.         if( it.first == p || f )continue ;
  39.         if( vis[it.first] != clr )
  40.             dfs( it.first , nd ) ;
  41.         else
  42.         {
  43.             while( st.top() != it.first )
  44.             {
  45.                 int x = st.top() ;
  46.                 st.pop() ;
  47.                 cycle.push_back( x ) ;
  48.             }
  49.             cycle.push_back( it.first ) ;
  50.             f = 1;
  51.             return ;
  52.         }
  53.     }
  54.     if( f ) return ;
  55.     st.pop() ;
  56. }
  57.  
  58. void trav( int nd , int p , int hd , ll c = 0 , int d = 0 )
  59. {
  60.     dep[nd] = d ;
  61.     vis[nd] = clr ;
  62.     head[nd] = hd ;
  63.     par[nd][0] = p ;
  64.     cost[nd] = min( cost[nd] , c ) ;
  65.     for( auto it : adj[nd] )
  66.         if( it.first == p || num[it.first] || vis[it.first] == clr ) continue ;
  67.         else
  68.             trav( it.first , nd , hd , c+it.second , d+1 ) ;
  69. }
  70.  
  71. int LCA(int u , int v)
  72. {
  73.     if(dep[u] < dep[v])
  74.         swap(u,v) ;
  75.     int dist = dep[u] - dep[v] ;
  76.     while(dist > 0)
  77.     {
  78.         int raise_by = log2(dist);
  79.         u = par[u][raise_by];
  80.         dist -= (1<<raise_by) ;
  81.     }
  82.  
  83.     if(u == v)
  84.     {
  85.         return u ;
  86.     }
  87.     for(int j = MXLOG ; j >= 0 ; --j)
  88.     {
  89.         if((par[u][j] != -1) && (par[u][j] != par[v][j]))
  90.         {
  91.             u = par[u][j] ;
  92.             v = par[v][j] ;
  93.         }
  94.     }
  95.     return par[u][0] ;
  96. }
  97.  
  98. void calc( int nd , int d )
  99. {
  100.     num[nd] = d ;
  101.     if( d == cycle.size() ){
  102.         for( auto it : adj[nd] )
  103.         if( cycle[0] == it.first )
  104.             pref[d] = it.second ;
  105.             return ;
  106.     }
  107.     for( auto it : adj[nd] )
  108.         if( cycle[d] == it.first )
  109.         {
  110.             pref[d] = it.second ;
  111.             calc( it.first , d+1 ) ;
  112.         }
  113. }
  114. /*
  115. 2
  116. 7 6
  117. 1 2 2
  118. 1 3 5
  119. 2 3 1
  120. 2 4 1
  121. 2 5 1
  122. 3 6 1
  123. 3 7 1
  124. 1
  125. 9 10
  126. 1 2 3
  127. 1 3 4
  128. 2 6 1
  129. 2 7 7
  130. 3 4 2
  131. 3 5 1
  132. 5 8 6
  133. 5 9 3
  134. 7 6 10
  135. */
  136.  
  137. int main()
  138. {
  139. //    freopen("out.txt" , "r",stdin) ;
  140. //    freopen("t.txt" , "w",stdout) ;
  141.     ll c ;
  142.     clr = 1 ;
  143.     int a , b , n , t , q ;
  144.     scanf("%d" , &t) ;
  145.     while( t-- )
  146.     {
  147.         init() ;
  148.         scanf("%d %d" , &n , &q) ;
  149.         while( n-- )
  150.         {
  151.             scanf("%d %d %lld" , &a , &b , &c) ;
  152.             adj[a].push_back({b,c}) ;
  153.             adj[b].push_back({a,c}) ;
  154.         }
  155.         dfs(1) ;
  156.         reverse( cycle.begin() , cycle.end() ) ;
  157.         for( auto it : cycle )
  158.             num[it] = ++cyc_len ;
  159.         calc(cycle[0],1) ;
  160.  
  161.         for( int i = 1 ; i <= cyc_len ; ++i )
  162.             pref[i]+=pref[i-1] ;
  163.  
  164.         for( auto it : cycle )
  165.         {
  166.             clr++ ;
  167.             trav( it , -1 , it ) ;
  168.         }
  169.         for(int j = 1; (1<<j) < MX ; ++j)
  170.             for(int i = 1 ; i < MX ; ++i)
  171.                 if(par[i][j-1] != -1)
  172.                     par[i][j] = par[ par[i][j-1] ][j-1] ;
  173.  
  174.         while( q-- )
  175.         {
  176.             ll ans = 0 ;
  177.             scanf("%d %d" , &a , &b) ;
  178.             if( head[a] != head[b] )
  179.             {
  180.                 ans = cost[a] + cost[b] ;
  181.                 b = num[ head[b] ] ;
  182.                 a = num[ head[a] ] ;
  183.                 if( a > b ) swap( a , b ) ;
  184.                 ll path_on_cyc = min( pref[ b - 1 ] - pref[ a - 1 ] , pref[cyc_len] - pref[ b - 1 ] +  pref[ a - 1 ]  ) ;
  185.                 ans+=path_on_cyc ;
  186.             }
  187.             else
  188.             {
  189.                 int lca = LCA( a , b ) ;
  190.                 ans = cost[a] + cost[b] - 2ll*cost[lca] ;
  191.             }
  192.         printf("%lld\n",ans) ;
  193.         }
  194.     }
  195.     return 0 ;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment