Skip to content

链式前向星 ​

c++
const int N = 100005; // 最大节点数
const int M = 200005; // 最大边数(注意:无向图必须开 2 倍)

int head[N], to[M], nxt[M], w[M], tot;

void init(int n) {
    std::fill(head, head + n + 1, -1);
    tot = 0;
}

void add_edge(int u, int v, int weight = 0) {
    to[tot] = v;    
    w[tot] = weight;
    nxt[tot] = head[u];
    head[u] = tot++;
}