TOJ 1607 Distance Queries -- LCA在线算法 RMQ

题目链接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1607

分析:LCA问题,转化为RMQ问题,可以看看一份非常好的学习资料:

http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
         #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #define mp make_pair #define X first #define Y second #define MEMSET(a, b) memset(a, b, sizeof(a)) using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; typedef pair 
                   
                     pii; typedef vector 
                    
                      vi; typedef vi::iterator vi_it; typedef map 
                     
                       mii; typedef priority_queue 
                      
                        pqi; typedef priority_queue 
                       
                         , greater 
                        
                          > rpqi; typedef priority_queue 
                         
                           pqp; typedef priority_queue 
                          
                            , greater 
                           
                             > rpqp; const int MAX_N = 40000 + 2; vector 
                            
                              link[MAX_N]; int dis[MAX_N]; int H[MAX_N]; int cnt = 0, depth = 0; int dp[MAX_N << 1][18]; int E[MAX_N << 1]; void make_rmp(int n) { for (int j = 1; (1 << j) <= n; ++j) { for (int i = 1; i + (1 << j) - 1 <= n; ++i) { dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]); } } } int rmq_min(int s, int t) { int k = (int)(log((double)(t - s + 1)) / log(2.0)); return min(dp[s][k], dp[t - (1 << k) + 1][k]); } inline void add_edge(int u, int v, int w) { link[u].push_back(mp(v, w)); link[v].push_back(mp(u, w)); } void dfs(int u, int fa) { int tmp = dp[H[u] = ++cnt][0] = ++depth; E[tmp] = u; for (vector 
                             
                               ::iterator it = link[u].begin(); it != link[u].end(); ++it) { if (it->X != fa) { dis[it->X] = dis[u] + it->Y; dfs(it->X, u); dp[++cnt][0] = tmp; } } } int dist(int u, int v) { int lca = E[rmq_min(min(H[u], H[v]), max(H[u], H[v]))]; return dis[u] + dis[v] - dis[lca] - dis[lca]; } int main(int argc, char *argv[]) { // freopen("D:\\in.txt", "r", stdin); int n, m, k, a, b, t; char ch; cin >> n >> m; while (m--) { scanf("%d%d%d %c", &a, &b, &t, &ch); add_edge(a, b, t); } dfs(1, 1); make_rmp(cnt); cin >> k; while (k--) { scanf("%d%d", &a, &b); printf("%d\n", dist(a, b)); } return 0; } 
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
       
      
      
     
     
    
    
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值