标签:
/* * Author: sweat123 * Created Time: 2016/7/13 8:53:48 * File Name: main.cpp */ #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<time.h> #include<cstring> #include<iostream> #include<algorithm> #define INF 1<<30 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define key_value ch[ch[root][1]][0] #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 40010; struct node{ int to; int val; int next; }edge[MAXN*2]; int dp[MAXN*2][20],n,m,ind,pre[MAXN],vis[MAXN]; int dfn[MAXN*2],first[MAXN],ver[MAXN*2],tot; ll dis[MAXN]; //dfn 表示深度 first第一次出现这个点的下标 dis长度 ver先序访问的节点编号 tot编号个数 void add(int x,int y,int z){ edge[ind].to = y; edge[ind].val = z; edge[ind].next = pre[x]; pre[x] = ind ++; } void dfs(int rt,int dep){ vis[rt] = 1; ver[++tot] = rt; dfn[tot] = dep; first[rt] = tot; for(int i = pre[rt]; i != -1; i = edge[i].next){ int t = edge[i].to; if(!vis[t]){ dis[t] = dis[rt] + edge[i].val; dfs(t,dep+1); ver[++tot] = rt;//先序访问 dfn[tot] = dep; } } } void rmq(){ for(int i = 1; i <= tot; i++){ dp[i][0] = i; } for(int i = 1; i < 20; i++){ for(int j = 1; j + (1 << i) - 1 <= tot; j++){ if(dfn[dp[j][i-1]] > dfn[dp[j+(1<<(i-1))][i-1]]){ dp[j][i] = dp[j+(1<<(i-1))][i-1]; } else { dp[j][i] = dp[j][i-1]; } } } } int askrmq(int x,int y){ x = first[x]; y = first[y]; if(x > y)swap(x,y); int k = (int)(log(y - x + 1) * 1.0 / log(2.0)); return min(dp[x][k],dp[y-(1<<k)+1][k]); } int main(){ int t,flag = 0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); ind = 0; tot = 0; memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); memset(pre,-1,sizeof(pre)); for(int i = 1; i < n; i++){ int x,y,z; scanf("%d%d%d",&x,&y,&z); add(x,y,z),add(y,x,z); } dfs(1,1); rmq(); if(flag)puts(""); flag = 1; while(m--){ int x,y; scanf("%d%d",&x,&y); int tp = ver[askrmq(x,y)]; ll ans = dis[x] - dis[tp] + dis[y] - dis[tp]; printf("%lld\n",ans); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/5665899.html