LCA - 倍增法去求第几个节点

You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. Each edge has an integer value assigned to it, representing its length.

We will ask you to perfrom some instructions of the following form:

  • DIST a b : ask for the distance between node a and node b
    or
  • KTH a b k : ask for the k-th node on the path from node a to node b

Example:
N = 6
1 2 1 // edge connects node 1 and node 2 has cost 1
2 4 1
2 5 2
1 3 1
3 6 2

Path from node 4 to node 6 is 4 -> 2 -> 1 -> 3 -> 6
DIST 4 6 : answer is 5 (1 + 1 + 1 + 2 = 5)
KTH 4 6 4 : answer is 3 (the 4-th node on the path from node 4 to node 6 is 3)

Input

The first line of input contains an integer t, the number of test cases (t <= 25). t test cases follow.

For each test case:

  • In the first line there is an integer N (N <= 10000)
  • In the next N-1 lines, the i-th line describes the i-th edge: a line with three integers a b c denotes an edge between a, b of cost c (c <= 100000)
  • The next lines contain instructions "DIST a b" or "KTH a b k"
  • The end of each test case is signified by the string "DONE".

There is one blank line between successive tests.

Output

For each "DIST" or "KTH" operation, write one integer representing its result.

Print one blank line after each test.

Example

Input:
1

6
1 2 1
2 4 1
2 5 2
1 3 1
3 6 2
DIST 4 6
KTH 4 6 4
DONE

Output:
5
3

题目分析 : 两种操作,一是查看任意两点间的距离,二是查询从a到b第k个点是什么
思路分析 : 裸的 LCA ,对于第二种操作,由于倍增中存的就是跳跃的点,所以查看跳到的第几个点也是很容易的
代码示例 :
#define ll long long
const ll maxn = 1e4+5;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;

struct node
{
    ll to, cost;
    node(ll _to = 0, ll _cost = 0):to(_to), cost(_cost){}
};
vector<node>ve[maxn];
ll dep[maxn];
ll grand[maxn][20], gw[maxn][20];
ll N;

void dfs(ll x, ll fa){
    for(ll i = 1; i <= N; i++){
        grand[x][i] = grand[grand[x][i-1]][i-1];
        gw[x][i] = gw[x][i-1] + gw[grand[x][i-1]][i-1];
    }
    for(ll i = 0; i < ve[x].size(); i++){
        ll to = ve[x][i].to;
        ll cost = ve[x][i].cost;
        if (to == fa) continue;
        grand[to][0] = x;
        gw[to][0] = cost;
        dep[to] = dep[x] + 1;
        dfs(to, x); 
    }
}

ll ans = 0;
ll a, b, c;
ll lca(){
    if (dep[a] > dep[b]) swap(a, b);
    
    ans = 0;
    for(ll i = N; i>= 0; i--){ 
        if (dep[a] < dep[b] && dep[grand[b][i]] >= dep[a]) {
            ans += gw[b][i];
            b = grand[b][i]; 
        }
    }
    for(ll i = N; i >= 0; i--){
        if (grand[a][i] != grand[b][i]) {
            ans += gw[a][i];
            ans += gw[b][i];
            a = grand[a][i], b = grand[b][i];
        }
    }
    if (a != b) return grand[a][0];
    else return a;
}

ll fid(ll a, ll b, ll f) {
    if (dep[a]-dep[f]+1 >= c){
        ll len = dep[a] - c + 1;
        if (len == 0) return 1;
        for(ll i = N; i >= 0; i--){
            if (dep[grand[a][i]] >= len){
                a = grand[a][i];
            }
        }
        return a;
    }   
    else {
        ll len = c - (dep[a]-dep[f]+1) + dep[f];
        for(ll i = N; i >= 0; i--){
            if (dep[grand[b][i]] >= len){
                b = grand[b][i];
            }
        }
        return b;
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ll t, n;
    char s[20];
    ll p1, p2;
    
    cin >> t;
    while(t--){
        scanf("%lld", &n);
        for(ll i = 1; i <= 10000; i++) ve[i].clear();
        for(ll i = 1; i< n; i++){
            scanf("%lld%lld%lld", &a, &b, &c);    
            ve[a].push_back(node(b, c));
            ve[b].push_back(node(a, c));
        }
        N = floor(log(n)/log(2)); 
        memset(grand, 0, sizeof(grand));
        memset(gw, 0, sizeof(gw));
        dep[1] = 0;
        dfs(1, 1); 
        while(1){
            scanf("%s", s);
            if (s[1] == 'O') break;
            else if (s[1] == 'I') {
                scanf("%lld%lld", &a, &b);
                ll f = lca();
                if (a != b) {
                    ans += gw[a][0];
                    ans += gw[b][0];
                }   
                printf("%lld\n", ans);
            }
            else if (s[1] == 'T'){
                scanf("%lld%lld%lld", &a, &b, &c);
                p1 = a, p2 = b;
                ll f = lca();
                printf("%lld\n", fid(p1, p2, f));
            }
        }    
    }
    return 0;
}
/*
10
8
1 2 1
1 3 2
1 7 3
2 4 4
2 5 5
3 6 6
7 8 7
KTH 4 8 3
DIST
*/

 

posted @ 2018-02-07 15:10  楼主好菜啊  阅读(161)  评论(0编辑  收藏  举报