{ } </> O(n) // [ ] *p log &&

TRUNK TRICK

Trunk Trick

An undergraduate at Xi'an Jiaotong University, an independent developer, and a competitive programmer obsessed with the craft of algorithms.

From number theory to graph theory, from data structures to dynamic programming — this is where I document every "aha!" moment along the journey. Built slowly, written carefully.

// Lowest Common Ancestor
int lca(int u, int v) {
    if (depth[u] < depth[v]) swap(u, v);
    int diff = depth[u] - depth[v];
    for (int i = 0; diff; i++, diff >>= 1)
        if (diff & 1) u = up[u][i];
    if (u == v) return u;
    for (int i = LOG-1; i >= 0; i--)
        if (up[u][i] != up[v][i])
            u = up[u][i], v = up[v][i];
    return up[u][0];
}

Interactive

Branch & Bound on TSP

Reduced Matrix of parent (V1)
10068 0090 40812 8510 01160
Pick edge
V1 → V2
Discard row/col
Initial Matrix of child
090 4812 810 060
Row & col
reduction
Reduced Matrix of child
090 048 810 060
Global base cost
Parent's reduced cost (12)
Extra cost of chosen edge
Edge V1→V2 weight (10)
Minimum additional cost
Child matrix re-reduction (4+0)
lb = lb(parent) + w(V1→V2) + child.reduced = 12 + 10 + 4 = 26
Ready — click Expand
V1
12
V2
26
V3
12
V4
18
V5
21
V2
12
V4
25
V5
29
V2
23
V3
19
V5
24
V4
21
V5
20
V2
19
V5
31
V5
19
π λ

TOPICS

Topics I Write About

Every topic here comes from real problems I've encountered. The visualizations are my way of making abstract ideas concrete.

Max Flow Modeling: Baseball Elimination
Graph Theory · Network Flow

Max Flow Modeling

How do you determine whether a sports team is mathematically eliminated from the playoffs? The answer is a maximum flow network — source nodes representing remaining games, team nodes with carefully chosen capacities, and a sink that reveals the truth.

Planar Graphs and Euler's Formula
Graph Theory · Planarity

Planar Graphs & Kuratowski's Theorem

Which graphs can be drawn on a plane without crossing edges? From Euler's formula $V - E + F = 2$ to the forbidden subgraphs $K_5$ and $K_{3,3}$, planarity theory sits at the intersection of discrete mathematics, topology, and computational geometry.

// Disjoint Set Union
int find(int x) {
    return p[x] == x ? x : p[x] = find(p[x]);
}
void unite(int a, int b) {
    a = find(a), b = find(b);
    if (a != b) {
        if (sz[a] < sz[b]) swap(a, b);
        p[b] = a; sz[a] += sz[b];
    }
}
Data Structures · DSU

Disjoint Set Union & Path Compression

The Union-Find data structure with path compression achieves nearly $O(1)$ amortized time per operation. Deceptively simple — just a few lines of code — yet it's the backbone of Kruskal's MST, dynamic connectivity, and countless competitive programming solutions.

Interactive