跳到正文

个人记录

vector,链前

GX6zm约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月13日
更新
2026年5月19日
篇幅
约 11 分钟

版本

  1. v1当前

graph.dat

C++
// gen_graph.cpp
#include <bits/stdc++.h>
using namespace std;

inline int rd() {
    int x = 0, f = 1; char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
    while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int randint(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

int main() {
    const int n = 100000;
    const int m = 4500000;
    const bool allow_self = true;
    const bool allow_dup = true;

    FILE* fout = fopen("graph.dat", "w");
    if (!fout) { perror("fopen"); return 1; }

    fprintf(fout, "%d %d\n", n, m);

    if (!allow_dup) {
        unordered_set<long long> S;
        S.reserve(m * 2);
        auto pack = [](int u, int v) { return (1LL * u << 32) + v; };
        int cnt = 0;
        while (cnt < m) {
            int u = randint(1, n);
            int v = randint(1, n);
            if (!allow_self && u == v) continue;
            if (S.insert(pack(u, v)).second) {
                fprintf(fout, "%d %d\n", u, v);
                ++cnt;
            }
        }
    } else {
        for (int i = 0; i < m; ++i) {
            int u = randint(1, n);
            int v = randint(1, n);
            if (!allow_self && u == v)
                v = (v == n ? 1 : v + 1);
            fprintf(fout, "%d %d\n", u, v);
        }
    }

    fclose(fout);
    printf("? 图已生成并保存至 'graph.dat'|顶点数:%d|边数:%d\n", n, m);
    return 0;
}

test_cs_bfs.cpp

C++
// test_cs_bfs.cpp
#include <bits/stdc++.h>
using namespace std;

struct GraphCS {
    int *head, *to, *nxt;
    int idx, N, M;
    GraphCS(int n, int m) : N(n), M(m), idx(0) {
        head = new int[N + 1]();
        to   = new int[M + 1];
        nxt  = new int[M + 1];
        for (int i = 0; i <= N; ++i) head[i] = -1;
    }
    ~GraphCS() {
        delete[] head; delete[] to; delete[] nxt;
    }
    inline void add(int u, int v) {
        to[idx] = v;
        nxt[idx] = head[u];
        head[u] = idx++;
    }
};

int main() {
    FILE* fin = fopen("graph.dat", "r");
    if (!fin) { perror("打开 graph.dat 失败"); return 1; }

    int n, m;
    fscanf(fin, "%d %d", &n, &m);
    vector<pair<int, int>> edges;
    edges.reserve(m);
    for (int i = 0; i < m; ++i) {
        int u, v; fscanf(fin, "%d %d", &u, &v);
        edges.emplace_back(u, v);
    }
    fclose(fin);

    // —————— 建图计时 ——————
    auto t1 = chrono::steady_clock::now();
    GraphCS g(n, m);
    for (auto &e : edges) g.add(e.first, e.second);
    auto t2 = chrono::steady_clock::now();

    // —————— 真实 BFS 计时 ——————
    vector<char> vis(n + 1, 0);  // char 比 bool 更 cache-friendly
    queue<int> q;
    long long visited_count = 0;

    auto t3 = chrono::steady_clock::now();

    // 多连通分量支持:遍历所有未访问点(模拟完整图遍历)
    for (int start = 1; start <= n; ++start) {
        if (vis[start]) continue;
        q.push(start);
        vis[start] = 1;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            ++visited_count;
            for (int i = g.head[u]; ~i; i = g.nxt[i]) {
                int v = g.to[i];
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }

    auto t4 = chrono::steady_clock::now();

    double build_ms = chrono::duration<double, milli>(t2 - t1).count();
    double bfs_ms   = chrono::duration<double, milli>(t4 - t3).count();

    printf("【链式前向星 + 真实 BFS】\n");
    printf("顶点数:%d|访问顶点数:%lld(应 = %d)\n", n, visited_count, n);
    printf("建图耗时:%.2f ms\n", build_ms);
    printf("BFS 耗时:%.2f ms\n", bfs_ms);
    return 0;
}

test_vec_bfs.cpp

C++
// test_vec_bfs.cpp
#include <bits/stdc++.h>
using namespace std;

struct GraphVec {
    vector<vector<int>> g;
    GraphVec(int n) : g(n + 1) {}
    inline void add(int u, int v) { g[u].push_back(v); }
};

int main() {
    FILE* fin = fopen("graph.dat", "r");
    if (!fin) { perror("打开 graph.dat 失败"); return 1; }

    int n, m;
    fscanf(fin, "%d %d", &n, &m);
    vector<pair<int, int>> edges;
    edges.reserve(m);
    for (int i = 0; i < m; ++i) {
        int u, v; fscanf(fin, "%d %d", &u, &v);
        edges.emplace_back(u, v);
    }
    fclose(fin);

    // —————— 建图计时 ——————
    auto t1 = chrono::steady_clock::now();
    GraphVec g(n);
    for (auto &e : edges) g.add(e.first, e.second);
    auto t2 = chrono::steady_clock::now();

    // —————— 真实 BFS 计时 ——————
    vector<char> vis(n + 1, 0);
    queue<int> q;
    long long visited_count = 0;

    auto t3 = chrono::steady_clock::now();

    for (int start = 1; start <= n; ++start) {
        if (vis[start]) continue;
        q.push(start);
        vis[start] = 1;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            ++visited_count;
            for (int v : g.g[u]) {
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }

    auto t4 = chrono::steady_clock::now();

    double build_ms = chrono::duration<double, milli>(t2 - t1).count();
    double bfs_ms   = chrono::duration<double, milli>(t4 - t3).count();

    printf("【vector<vector> + 真实 BFS】\n");
    printf("顶点数:%d|访问顶点数:%lld(应 = %d)\n", n, visited_count, n);
    printf("建图耗时:%.2f ms\n", build_ms);
    printf("BFS 耗时:%.2f ms\n", bfs_ms);
    return 0;
}
原文