题目描述

【传送门】

题目大意

给一棵树,有两种操作:

  • 求(u,v)路径的距离。
  • 求以u为起点,v为终点的第k的节点.

分析

比较简单的倍增LCA模板题。
首先对于第一问,我们只需要预处理出根节点到各个节点之间的距离,然后倍增LCA求解就可以了。
那么第二问我WA了6发,原来是眼瞎和手残打错了两个字符错掉了。
我们将问题分成3个部分:

  • LCA是第k个
  • 第k个在u到LCA的路径上
  • 第k个在LCA到v的路径上。

首先如果LCA是第k个,那么直接输出。
如果是第二种情况,那么从u开始做倍增,每一次k-(1<<i)就可以了。
小细节:只能将k变成1,模拟可证明。
第三种情况,那么我们的答案就是从v的第(dep[u]+dep[v]-2*dep[lca]+2),模拟可得该式。

ac代码

#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
template <typename T>
inline void read(T &x) {
    x = 0; T fl = 1;
    char ch = 0;
    while (ch < '0' || ch > '9') {
        if (ch == '-') fl = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x *= fl;
}
#define N 20005
struct edge {
    int to, nt;
    ll w;
}E[N << 1];
ll dis[N];
int f[N][31], H[N], dep[N];
int cnt, n;
void add_edge(int u, int v, ll w) {
    E[++ cnt] = (edge){v, H[u], w};
    H[u] = cnt;
}
void dfs(int u, int fa, int dist) {
    f[u][0] = fa;
    dis[u] = dist;
    for (int i = 1; i <= 30; i ++)
        f[u][i] = f[f[u][i - 1]][i - 1];
    for (int e = H[u]; e; e = E[e].nt) {
        int v = E[e].to;
        if (v == fa) continue;
        dep[v] = dep[u] + 1;
        dfs(v, u, dist + E[e].w);
    }
}
int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    for (int i = 30; i >= 0; i --)
        if (dep[v] <= dep[f[u][i]]) u = f[u][i];
    if (u == v) return u;
    for (int i = 30; i >= 0; i --)
        if (f[u][i] != f[v][i]) {
            u = f[u][i];
            v = f[v][i];
        }
    return f[u][0];
}
int solve(int u, int v, int k) {
    int Lca = lca(u, v);
    if (dep[u] - dep[Lca] + 1 == k) return Lca;
    else {
        if (dep[u] - dep[Lca] + 1 > k) {
            for (int i = 30; i >= 0; i --)
                if (k - 1 >= (1 << i)) k -= (1 << i), u = f[u][i];
            return u;
        }
        else {
            k = dep[u] + dep[v]- dep[Lca] * 2 - k + 2;
            for (int i = 30; i >= 0; i --)
                if (k - 1 >= (1 << i)) k -= (1 << i), v = f[v][i];
            return v;
        }
    }
}
int main() {
    int cas;
    read(cas);
    char opt[10];
    while (cas --) {
        cnt = 0;
        ms(H, 0);
        ms(dis, 0);
        ms(dep, 0);
        ms(f, 0);
        read(n);
        for (int i = 1; i < n; i ++) {
            int u, v;  ll w;
            read(u); read(v); read(w);
            add_edge(u, v, w);
            add_edge(v, u, w);
        }
        dep[1] = 1;
        dfs(1, 0, 0);
        while (1) {
            scanf("%s", opt);
            if (opt[1] == 'O') break;
            if (opt[0] == 'D') {
                int u, v;
                read(u); read(v);
                int Lca = lca(u, v), res;
                printf("%lld\n", dis[u] + dis[v] - dis[Lca] * 2);
            }
            else {
                int u, v, k;
                read(u); read(v); read(k);
                printf("%d\n", solve(u, v, k));
            }
        }
    }
    return 0;
}

[SPOJ913]QTREE2 - Query on a tree II【倍增LCA】的更多相关文章

  1. spoj 913 Query on a tree II (倍增lca)

    Query on a tree II You are given a tree (an undirected acyclic connected graph) with N nodes, and ed ...

  2. 【SPOJ QTREE2】QTREE2 - Query on a tree II(LCA)

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  3. Query on a tree II 倍增LCA

    You are given a tree (an undirected acyclic connected graph) with N nodes, and edges numbered 1, 2, ...

  4. LCA SP913 QTREE2 - Query on a tree II

    SP913 QTREE2 - Query on a tree II 给定一棵n个点的树,边具有边权.要求作以下操作: DIST a b 询问点a至点b路径上的边权之和 KTH a b k 询问点a至点 ...

  5. SP913 QTREE2 - Query on a tree II

    思路 第一个可以倍增,第二个讨论在a到lca的路径上还是lca到b的路径上, 倍增即可 代码 #include <cstdio> #include <algorithm> #i ...

  6. SPOJ QTREE2 Query on a tree II

    传送门 倍增水题…… 本来还想用LCT做的……然后发现根本不需要 //minamoto #include<bits/stdc++.h> using namespace std; #defi ...

  7. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  8. QTREE2 spoj 913. Query on a tree II 经典的倍增思想

    QTREE2 经典的倍增思想 题目: 给出一棵树,求: 1.两点之间距离. 2.从节点x到节点y最短路径上第k个节点的编号. 分析: 第一问的话,随便以一个节点为根,求得其他节点到根的距离,然后对于每 ...

  9. SPOJ913 Query on a tree II

    Time Limit: 433MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are g ...

随机推荐

  1. 替换iframe的内容

    一般就是点个按钮然后在不跳转页面的情况下显示另外一个页面的内容,显示的速度比较快,ifream还算是常用的吧 用的时候实现方式有以下几种, 1.替换src路径(觉得麻烦,没接触过,就不这样干了) 2. ...

  2. Linux下查看文件系统磁盘使用

    [root@localhost ~]# df -h 可以查看所有文件系统的磁盘使用情况 du --max-depth=1 -h 可以查看当前目录下各子目录的磁盘使用情况 参考:http://www.2 ...

  3. JavaScript中的各种X,Y,Width,Height

    在JavaScript DOM编程中,会接触很多很多很多关于浏览器的宽高,屏幕的宽高,元素的各种宽高,以及鼠标的坐标等,常常让人搞混.索性就写篇博客整理一下. case 1:鼠标的坐标 获取鼠标的坐标 ...

  4. hadoop实例-网站用户行为分析

    一.数据集 网站用户购物行为数据集2030万条,包括raw_user.csv(2000万条)和small_user.csv(30万条,适合新手) 字段说明: user_id 用户编号,item_id ...

  5. linux下编译upx ucl

    昨天,UPX发布了3.93版本. UPX(the Ultimate Packer for eXecutables)是一个非常全面的可执行文件压缩软件,支持dos/exe.dos/com.dos/sys ...

  6. 【Python3练习题 002】企业发放的奖金根据利润提成

    # [Python练习题 002]企业发放的奖金根据利润提成.# 利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分, ...

  7. h5 文件下载

    一.a 标签 移动端不支持 onDownFile= (url, filename) => { const downUrl = `http://10.1.109.123:19092/down/to ...

  8. Flutter的输入框TextField

    TextFiled组件的API 先来看一下TextFiled的构造方法: const TextField({ Key key, this.controller, this.focusNode, thi ...

  9. js对字符串的一些操作方法

    1.charCodeAt(index); 返回一个整数,代表下标位置上字符的Unicode的编码. 2.fromCharCode(code1,code2,code3,...); code1代表Unic ...

  10. 集合之HashSet(含JDK1.8源码分析)

    一.前言 我们已经分析了List接口下的ArrayList和LinkedList,以及Map接口下的HashMap.LinkedHashMap.TreeMap,接下来看的是Set接口下HashSet和 ...