SPOJ-QTREE2 Query on a tree II(暴力+LCA)

题目大意:给出一棵树,3种操作

DIST u,v 询问u到v的距离

KTH k, u, v 询问u到v的路径上的第k大的边的权值

解题思路:刚开始以为会爆,结果发现不会

直接暴力存储u到v的路上的所有边,再进行排序,输出第k大的边即可

#include <cstdio>
#include <cstring>

#define N 10010

struct Edge{
    int to, next, cost;
}E[2*N];

int head[N], depth[2 * N], first[N], ver[2 * N], pre[N], dis[N], dp[2*N][63];
int n, tot;
bool vis[N];
char str[100];

void AddEdge(int u, int v, int c) {
    E[tot].to = v; E[tot].next = head[u]; E[tot].cost = c; head[u] = tot++;
    u = u ^ v; v = u ^ v; u = u ^ v;
    E[tot].to = v; E[tot].next = head[u]; E[tot].cost = c; head[u] = tot++;
}

void init() {
    scanf("%d", &n);
    memset(head, -1, sizeof(head));
    tot = 0;

    int u, v, c;
    for (int i = 1; i < n; i++) {
        scanf("%d%d%d", &u, &v, &c);
        AddEdge(u, v, c);
    }
}

void dfs(int u, int dep) {
    vis[u] = true; ver[++tot] = u; depth[tot] = dep; first[u] = tot;
    for (int i = head[u]; ~i; i = E[i].next) {
        int v = E[i].to;
        if (!vis[v]) {
            pre[v] = u;
            dis[v] = dis[u] + E[i].cost;
            dfs(v, dep + 1);
            ver[++tot] = u; depth[tot] = dep;
        }
    }
}

void RMQ() {
    for (int i = 1; i <= tot; i++)
        dp[i][0] = i;

    int a, b;
    for (int j = 1; (1 << j) <= tot; j++)
        for (int i = 1; i + (1 << j) - 1 <= tot; i++) {
            a = dp[i][j - 1];
            b = dp[i + (1 << (j - 1))][j-1];
            if (depth[a] < depth[b])
                dp[i][j] = a;
            else
                dp[i][j] = b;
        }
}

int Query(int x, int y) {
    int k = 0;
    while (1 << (k + 1) <= (y - x + 1)) k++;
    int a = dp[x][k];
    int b = dp[y - (1 << k) + 1][k];
    if (depth[a] < depth[b])
        return a;
    return b;
}

int LCA(int u, int v) {
    int a = first[u];
    int b = first[v];
    if (a > b) {
        a = a ^ b; b = a ^ b; a = a ^ b;
    }
    int c = Query(a, b);
    return ver[c];
}

int num[N];
int KTH(int u, int v, int c) {
    int lca = LCA(u, v);
    int cnt = 1;

    int t = u;
    while (t != lca) {
        t = pre[t];
        cnt++;
        if (cnt == c)
            return t;
    }
    c = c - cnt;
    t = v;
    int cnt2 = 0;

    while (t != lca) {
        num[cnt2] = t;
        cnt2++;
        t = pre[t];
    }
//  printf("cnt2 is %d c is %d \n", cnt2, c);
    return num[cnt2 - c];
}

void solve() {
    memset(vis, 0, sizeof(vis));
    tot = 0;
    dis[1] = 0;
    pre[1] = 1;
    dfs(1,1);
    RMQ();
    int u, v, c;
    while (scanf("%s", str)) {
        if (str[0] == ‘D‘ && str[1] == ‘O‘)
            break;
        if (str[0] == ‘D‘) {
            scanf("%d%d", &u, &v);
            int lca = LCA(u, v);
            printf("%d\n", dis[u] + dis[v] - 2 * dis[lca]);
        }
        else {
            scanf("%d%d%d", &u, &v, &c);
            if (c == 1)
                printf("%d\n", u);
            else
                printf("%d\n", KTH(u, v, c));
        }
    }

}

int main() {
    int test;
    scanf("%d", &test);
    while (test--) {
        init();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 21:18:18

SPOJ-QTREE2 Query on a tree II(暴力+LCA)的相关文章

SPOJ QTREE2 Query on a tree II

Query on a tree II Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: QTREE264-bit integer IO format: %lld      Java class name: Main You are given a tree (an undirected acyclic connected graph) with N nodes,

【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...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: D

SPOJ 913 QTREE系列- Query on a tree II (倍增LCA)

题目地址:QTREE2 - Query on a tree II LCA学了离线与在线转RMQ方法后就去做这道题,于是想了好长时间也没想到怎么做.看了题解都是用的倍增LCA..于是又去学了下倍增法求LCA,这才发现用倍增法做简直是水题...因为求路径的第k个点可以转化成求第k个父节点,然而倍增法的原理就是根据的父节点,于是这题就很容易解决了.. 求距离很好求.关键是求路径第k个点,显然这个点要么是u的第k个父节点,要么是v的第k个父节点,于是乎,先求一次LCA,判断是u还是v的,然后用倍增法找到

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至点b有向路径上的第k个点的编号 有多组测试数据,每组数据以DONE结尾. 裸的LCA. 在处理第二个操作时,我直接向上数跳了多少个. 顾z大佬说不能这么做,要求出跳到那个点的深度再去跳. 真的是这样,不过懒得想了,应该是+1-1的误差. balabala... code: #include <iost

SPOJ 913 Query on a tree II ( 树链剖分 + 倍增 )

题目链接~~> 做题感悟:感觉又充实了一些. 解题思路:树链剖分 + 倍增 开始看时,第一问还好,第二问就不知道怎么解了.其实这两问都可以用倍增法解决. 先解释一下我理解的倍增 :记录 u 结点的 第 2 ^ i 个祖先,然后求u 的第 k 个祖先的时候,就相当于用 2 ^ i 去组合 k ,不断向上,一直到达第 k 个节点,其实每次更新的时k 的二进制中为 1 的位置.如下图,计算 u 的第 5 个祖先结点(这里不包括 u),先到达 u' 节点,然后再从 u' ,到 u'' (5 的二进制 1

SP913 QTREE2 - Query on a tree II

嘟嘟嘟 LCA水题,第二问看一下\(x\)到\(lca\)的路径长度是否够\(k - 1\),不过的话就从\(y\)出发往上跳. #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cstdlib> #include<cctype> #include<vector>

$[\ SPOJ\ 913\ ]\ Query\ on\ a\ tree\ II$

\(\\\) \(Description\) 给定一棵\(N\)个点的树,边有边权.作以下操作: \(DIST\ a\ b\) 询问点 \(a\) 至点 \(b\) 路径上的边权之和 \(KTH\ a\ b\ k\) 询问点 \(a\) 至点 \(b\) 有向路径上的第 \(k\) 个点的编号 有 \(T\) 组测试数据,每组数据以 \(DONE\) 结尾. \(T\in [1,25],N\in [1,10000]\) \(\\\) \(Solution\) \(LCA\) 裸题. 第一问可以

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...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: D

SPOJ 题目913QTREE2 - Query on a tree II(Link Cut Tree 查询路径第k个点)

QTREE2 - Query on a tree II no tags 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 i