跳到正文

个人记录

震惊!某 oier 发明了5行代码的树哈希?

monoid_me约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月14日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

省流:

C++
unsigned long long gethsh(int u,int f){
    unsigned long long ans=0;
    for(auto i:v[u]) if(i!=f) ans+=gethsh(i,u);
    return hash<bitset<128>>()((bitset<128>(ans)<<64)|bitset<128>(1));
}

当然,实际应用中这样写很难看,建议实际应用中这样写:

C++
using ull=unsigned long long;

ull hsh(ull x,ull y){
    using b=bitset<128>;
    return hash<b>()((b(x)<<64)|b(y));
}

ull gethsh(int u,int f){
    ull ans=0;
    for(auto i:v[u]) if(i!=f) ans+=gethsh(i,u);
    return hsh(ans,1);
}

下面是瞎扯时间

我的哈希理论:单点哈希,哈希合并

所谓单点哈希,指的是把一个数(int 或者 long long)通过 hash 运算得到另一个数(哈希值),这也是哈希函数的定义

所谓哈希合并,指的是将两个哈希值根据某种运算得到他们的哈希值

比如我们要哈希这个序列 {1,2} ,我们就可以先求出每一个数的哈希值(比如 1 的哈希值为 114514 , 2 的哈希值为1919810),然后我们通过某种运算将这两个哈希值合并成一个新的哈希值(比如 1234212)

实际上,我的这个理论没有被广泛采用的原因,是因为无法找到足够强的单点哈希,哈希合并函数

直到某一天,我发现了 std::hash

我迫不及待的使用了 hash<int> ,结果大失所望,因为这个函数在传入值很小的时候会直接返回传入值

直到又一天:

https://www.luogu.com.cn/discuss/569138

里面提到了 hash<string> 的底层算法

C++
  // Implementation of Murmur hash for 32-bit size_t.
  size_t
  _Hash_bytes(const void* ptr, size_t len, size_t seed)
  {
    const size_t m = 0x5bd1e995;
    size_t hash = seed ^ len;
    const char* buf = static_cast<const char*>(ptr);

    // Mix 4 bytes at a time into the hash.
    while(len >= 4)
      {
    size_t k = unaligned_load(buf);
    k *= m;
    k ^= k >> 24;
    k *= m;
    hash *= m;
    hash ^= k;
    buf += 4;
    len -= 4;
      }

    size_t k;
    // Handle the last few bytes of the input array.
    switch(len)
      {
      case 3:
    k = static_cast<unsigned char>(buf[2]);
    hash ^= k << 16;
    [[gnu::fallthrough]];
      case 2:
    k = static_cast<unsigned char>(buf[1]);
    hash ^= k << 8;
    [[gnu::fallthrough]];
      case 1:
    k = static_cast<unsigned char>(buf[0]);
    hash ^= k;
    hash *= m;
      };

    // Do a few final mixes of the hash.
    hash ^= hash >> 13;
    hash *= m;
    hash ^= hash >> 15;
    return hash;
  }

(当然,这个是 32 位机子上的算法,64 位机子上应该更复杂)

我突然意识到:将整数转为 string ,再 hash ,不就很强了吗?

但是,虽然这个哈希很强,常数却太大了

直接 调用 _Hash_bytes 会 CE

于是,这个事看似就吹了

然!而!,我发现 bitset 也有 hash ,它的哈希底层也是 _Hash_bytes !

所以就实现了单点哈希,哈希合并函数

C++
using ull=unsigned long long;

ull hsh(int x){return hash<bitset<64>>()(x);}
ull hsh(ull x,ull y){
    using b=bitset<128>;
    return hash<b>()((b(x)<<64)|b(y));
}

速度:(1s内)

不开 O2 :

hash(int): 7e7 次;hash(int,int): 1.8e7 次

开 O2 :

hash(int): 2.7×108\color{red}{2.7\times 10^8} 次;hash(int,int):2×108\color{red}{2\times 10^8} 次

具体为什么优化如此明显,请看:

O2好闪,拜谢O2!\color{red}{\text{O2好闪,拜谢O2!}}

既然有了这两个函数,什么都可以做了

序列哈希:

C++
ull hsh(vector<int> x){
	ull ans=0;
    for(auto i:x) ans=hsh(ans,i);
    return ans;
}

可重集哈希:(即要满足 hsh({1,2,3})==hsh({2,1,3}) )

C++
ull hsh(vector<int> x){
	ull ans1=0,ans2=0;
    for(auto i:x) ans+=hsh(i),ans2^=hsh(i);
    return hsh(ans1,ans2);
}

无点权树哈希:

C++
ull gethsh(int u,int f){
    ull ans=0;
    for(auto i:v[u]) if(i!=f) ans+=gethsh(i,u);
    return hsh(ans,1);
}

有点权树哈希:

C++
ull gethsh(int u,int f){
    ull ans=0;
    for(auto i:v[u]) if(i!=f) ans+=gethsh(i,u);
    return hsh(ans,val[u]);
}

总结:这样码量小、速度快、强度大的哈希,你值得拥有


如果有人可以卡掉任意一种哈希,奖励 5 RMB

先到先得

hack 成功可以私信我

原文
存档评论3 条抓取于