标签: LCA

[SCOI2016]幸运数字 题解

[SCOI2016]幸运数字 题解

题目地址:洛谷:【P3292】[SCOI2016]幸运数字 – 洛谷、BZOJ 

[SDOI2013]森林 题解

[SDOI2013]森林 题解

题目地址:洛谷:【P3302】[SDOI2013]森林 – 洛谷、BZOJ:P 

[NOIP2013提高]货车运输 题解

[NOIP2013提高]货车运输 题解

题目地址:洛谷:【P1967】货车运输 – 洛谷

题目描述

A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路。每一条道路对车辆都有重量限制,简称限重。现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重的货物。

输入输出格式

输入格式:
输入文件名为 truck.in。
输入文件第一行有两个用一个空格隔开的整数 n,m,表示 A 国有 n 座城市和 m 条道路。 接下来 m 行每行 3 个整数 x、 y、 z,每两个整数之间用一个空格隔开,表示从 x 号城市到 y 号城市有一条限重为 z 的道路。注意: x 不等于 y,两座城市之间可能有多条道路 。
接下来一行有一个整数 q,表示有 q 辆货车需要运货。
接下来 q 行,每行两个整数 x、y,之间用一个空格隔开,表示一辆货车需要从 x 城市运输货物到 y 城市,注意: x 不等于 y 。

输出格式:
输出文件名为 truck.out。
输出共有 q 行,每行一个整数,表示对于每一辆货车,它的最大载重是多少。如果货车不能到达目的地,输出-1。

输入输出样例

输入样例#1:

4 3
1 2 4
2 3 3
3 1 1
3
1 3
1 4
1 3

输出样例#1:

3
-1
3

说明

对于 30%的数据,0 < n < 1,000,0 < m < 10,000,0 < q< 1,000;
对于 60%的数据,0 < n < 1,000,0 < m < 50,000,0 < q< 1,000;
对于 100%的数据,0 < n < 10,000,0 < m < 50,000,0 < q< 30,000,0 ≤ z ≤ 100,000。

题解

暴力来做可以Floyd处理出两点路径上的最小边权。
我们考虑一个Kruskal算法的流程,如果是在求最大生成树,则按边权降序对边排序并加边,显然可以保证最大生成树上的路径就是最优解。因此我们把询问转换成了树上问题。
这与树链有关,所以其实我们可以选择使用树链剖分,将边权记在边较深的端点处,并线段树维护最小值。这样做的复杂度是O(n \log^2 n)的。
我们还可以用倍增思想,维护一个倍增路径上的最小权值。只需要在通常的倍增LCA算法上加一点小修改即可。这样做的复杂度是O(n \log n),比树剖更优。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cctype>

#include <algorithm>
#include <vector>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF 
        : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(!isdigit(c)) {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(isdigit(c)) {
        res = (res << 1) + (res << 3) + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 10005, MAXM = 50005;

int n, m;

struct Edge {
    int to, w;
};
std::vector<Edge> gra[MAXN];

struct Edge1 {
    int u, v, w;
} edges[MAXM];

inline bool cmp(Edge1 a, Edge1 b) {
    return a.w > b.w;
}

int fa[MAXN];

inline int find(int u) {
    return fa[u] == u ? u : fa[u] = find(fa[u]);
}

inline void kruskal() {
    int cnt = 0;
    for(int i = 1; i <= m; i++) {
        int f1 = find(edges[i].u), f2 = find(edges[i].v);
        if(f1 == f2) continue;
        fa[f2] = f1;
        gra[edges[i].u].push_back(Edge {edges[i].v, edges[i].w});
        gra[edges[i].v].push_back(Edge {edges[i].u, edges[i].w});
        cnt++;
        if(cnt >= n - 1) break;
    }
}

int anc[MAXN][25], mn[MAXN][25], dep[MAXN];

inline void dfs(int u) {
    for(auto e : gra[u]) {
        int v = e.to;
        if(v == anc[u][0]) continue;
        anc[v][0] = u;
        mn[v][0] = e.w;
        dep[v] = dep[u] + 1;
        dfs(v);
    }
}

inline void calanc() {
    for(int i = 1; i <= 20; i++) {
        for(int j = 1; j <= n; j++) {
            anc[j][i] = anc[anc[j][i - 1]][i - 1];
            mn[j][i] = std::min(mn[j][i - 1], mn[anc[j][i - 1]][i - 1]);
        }
    }
}

inline int query(int x, int y) {
    if(dep[x] > dep[y]) std::swap(x, y);
    int ans = 1e9;
    int del = dep[y] - dep[x];
    for(int i = 20; i >= 0; i--) {
        if(del & (1 << i)) {
            ans = std::min(ans, mn[y][i]);
            y = anc[y][i];
        }
    }
    if(x == y) return ans;
    for(int i = 20; i >= 0; i--) {
        if(anc[x][i] != anc[y][i]) {
            ans = std::min(ans, mn[x][i]);
            x = anc[x][i];
            ans = std::min(ans, mn[y][i]);
            y = anc[y][i];
        }
    }
    ans = std::min(ans, std::min(mn[x][0], mn[y][0]));
    return ans;
}

int q, x, y;

int main() {
    n = readint(); m = readint();
    for(int i = 1; i <= n; i++) {
        fa[i] = i;
    }
    for(int i = 1; i <= m; i++) {
        edges[i].u = readint(); edges[i].v = readint(); edges[i].w = readint();
    }
    std::sort(edges + 1, edges + m + 1, cmp);
    kruskal();
    for(int i = 1; i <= n; i++) {
        if(!dep[i]) {
            dep[i] = 1;
            dfs(i);
        }
    }
    calanc();
    q = readint();
    while(q--) {
        x = readint(); y = readint();
        int f1 = find(x), f2 = find(y);
        if(f1 != f2) puts("-1");
        else printf("%d\n", query(x, y));
    }
    return 0;
}
[USACO15DEC]最大流Max Flow 题解

[USACO15DEC]最大流Max Flow 题解

题目地址:洛谷:【P3128】[USACO15DEC]最大流Max Flow &#8211 

[SCOI2012]喵星球上的点名 题解

[SCOI2012]喵星球上的点名 题解

题目地址:洛谷:【P2336】[SCOI2012]喵星球上的点名 – 洛谷、B 

[SPOJ-QTREE2]Query on a tree II 题解

[SPOJ-QTREE2]Query on a tree II 题解

题目地址:洛谷:【SP913】QTREE2 – Query on a tree II – 洛谷、SPOJ:SPOJ.com – Problem QTREE2

SPOJ QTREE系列:

题目描述

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
  • KTH a b k : ask for the k-th node on the path from node a to node b

给一棵带边权的树,操作1.询问两点路径长2.求两点有向路径上第k点。

输入输出格式

输入格式:
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.

输出格式:
For each “DIST” or “KTH” operation, write one integer representing its result.
Print one blank line after each test.

输入输出样例

输入样例#1:

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

输出样例#1:

5
3

题解

求和同QTREE:[SPOJ-QTREE]Query on a tree 题解 | KSkun’s Blog。查k点可以考虑算一下LCA到两个儿子的距离,看看这个点在哪条链上,然后再换算成底端往上第几个点,沿重链上跳,利用DFS序算出来即可。

代码

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

#include <vector>
#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline int readint() {
    register int res = 0, neg = 1;
    register char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

inline bool isop(char c) {
    return c == 'I' || c == 'H' || c == 'O';
}

inline char readop() {
    register char c;
    while(!isop(c = fgc()));
    return c;
}

const int MAXN = 10005;

struct Edge {
    int to, w, nxt;
} gra[MAXN << 1];
int head[MAXN], tot;

int T, n, m, ut, vt, wt, kt;
char op;
int w[MAXN], fa[MAXN], siz[MAXN], son[MAXN], dfn[MAXN], ptn[MAXN], top[MAXN], dep[MAXN], cnt;

inline void dfs1(int u) {
    siz[u] = 1;
    son[u] = 0;
    for(register int i = head[u]; i; i = gra[i].nxt) {
        register int v = gra[i].to;
        if(v == fa[u]) continue;
        dep[v] = dep[u] + 1;
        fa[v] = u;
        w[v] = gra[i].w;
        dfs1(v);
        siz[u] += siz[v];
        if(siz[v] > siz[son[u]]) son[u] = v;
    }
}

inline void dfs2(int u, int tp) {
    top[u] = tp;
    dfn[u] = ++cnt;
    ptn[dfn[u]] = u;
    if(son[u]) dfs2(son[u], tp);
    for(register int i = head[u]; i; i = gra[i].nxt) {
        register int v = gra[i].to;
        if(v == son[u] || v == fa[u]) continue;
        dfs2(v, v);
    }
}

LL sgt[MAXN << 2];

inline void build(int o, int l, int r) {
    if(l == r) {
        sgt[o] = w[ptn[l]];
        return;
    }
    register int mid = (l + r) >> 1, lch = o << 1, rch = (o << 1) | 1;
    build(lch, l, mid);
    build(rch, mid + 1, r);
    sgt[o] = sgt[lch] + sgt[rch];
}

inline void modify(int o, int l, int r, int x, int v) {
    if(l == r) {
        sgt[o] = v;
        return;
    }
    register int mid = (l + r) >> 1, lch = o << 1, rch = (o << 1) | 1;
    if(x <= mid) modify(lch, l, mid, x, v);
    else modify(rch, mid + 1, r, x, v);
    sgt[o] = sgt[lch] + sgt[rch];
}

inline LL query(int o, int l, int r, int ll, int rr) {
    if(l >= ll && r <= rr) {
        return sgt[o];
    }
    register int mid = (l + r) >> 1, lch = o << 1, rch = (o << 1) | 1;
    register LL res = 0;
    if(ll <= mid) res += query(lch, l, mid, ll, rr);
    if(rr > mid) res += query(rch, mid + 1, r, ll, rr);
    return res;
}

inline LL querysum(int u, int v) {
    int tu = top[u], tv = top[v];
    register LL res = 0;
    while(tu != tv) {
        if(dep[tu] > dep[tv]) {
            std::swap(u, v);
            std::swap(tu, tv);
        }
        res += query(1, 1, n, dfn[tv], dfn[v]);
        v = fa[tv];
        tv = top[v];
    }
    if(dep[u] > dep[v]) std::swap(u, v);
    if(u != v) res += query(1, 1, n, dfn[u] + 1, dfn[v]);
    return res;
}

inline int querylca(int u, int v) {
    int tu = top[u], tv = top[v];
    while(tu != tv) {
        if(dep[tu] > dep[tv]) {
            std::swap(u, v);
            std::swap(tu, tv);
        }
        v = fa[tv];
        tv = top[v];
    }
    if(dep[u] > dep[v]) std::swap(u, v);
    return u;
}

inline int querykth(int u, int v, int k) {
    int lca = querylca(u, v), tu = top[u], tv = top[v];
    if(dep[u] - dep[lca] + 1 >= k) {
        while(dep[tu] > dep[lca]) {
            if(dep[u] - dep[tu] + 1 >= k) break;
            k -= dep[u] - dep[tu] + 1;
            u = fa[tu];
            tu = top[u];
        }
        return ptn[dfn[u] - k + 1];
    } else {
        k -= dep[u] - dep[lca] + 1;
        k = dep[v] - dep[lca] - k + 1;
        while(dep[tv] > dep[lca]) {
            if(dep[v] - dep[tv] + 1 >= k) break;
            k -= dep[v] - dep[tv] + 1;
            v = fa[tv];
            tv = top[v];
        }
        return ptn[dfn[v] - k + 1];
    }
}

inline void addedge(int u, int v, int w) {
    gra[++tot] = Edge {v, w, head[u]};
    head[u] = tot;
}

int main() {
    T = readint();
    while(T--) {
        tot = cnt = 0;
        memset(head, 0, sizeof(head));
        n = readint();
        for(int i = 1; i < n; i++) {
            ut = readint(); vt = readint(); wt = readint();
            addedge(ut, vt, wt);
            addedge(vt, ut, wt);
        }
        dfs1(1);
        dfs2(1, 1);
        build(1, 1, n);
        for(;;) {
            op = readop();
            if(op == 'O') break;
            ut = readint();
            vt = readint();
            if(op == 'I') {
                printf("%lld\n", querysum(ut, vt));
            } else {
                kt = readint();
                printf("%d\n", querykth(ut, vt, kt));
            }
        }
        printf("\n");
    }
    return 0;
}
树链剖分(轻重链剖分)原理与实现

树链剖分(轻重链剖分)原理与实现

概述 树链剖分是一种将树上的链划分为轻重链,从而实现降低复杂度的处理方式。剖分后的树,单次 

[SPOJ-COT]Count on a tree 题解

[SPOJ-COT]Count on a tree 题解

题目地址:洛谷:【SP10628】COT – Count on a tree