跳转至

树哈希

模版
C++
const long long mod = 1000000007;
const long long base = std::chrono::steady_clock::now().time_since_epoch().count() % mod + 1;
long long get(long long x) {
    x = x * x % mod;
    x = (x + base) % mod;
    x = x * x % mod;
    return x;
}
vector<int> adj[100005];
long long hashd[100005];
long long dfs(int u, int p) {
    hashd[u] = 1;
    for (auto &to : adj[u]) {
        if (to == p) continue;
        auto h = get(dfs(to, u));
        hashd[u] = (hashd[u] + h) % mod;
    }
    return hashd[u];
}
例题