树哈希板子+博文

博文
博文链接

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ull unsigned long long

int n;
ull h[2000005];
ull base=13331;//底数随便搞
vector <int> to[2000005];
ull H(ull x){
    
    
	return x*x*x*19890535+19260817;
}
ull F(ull x){
    
    
	return H(x&((1ll<<32)-1))+H(x>>32);
}
void dfs(int u,int fa)
{
    
    
	h[u]=1;
	for (int i=0;i<to[u].size();i++)
	{
    
    
		if (to[u][i]==fa) continue;
		dfs(to[u][i],u);
		h[u]+=F(h[to[u][i]]);	
	}
}
signed main()
{
    
    
	cin>>n;
	int a,b;
	for (int i=1;i<n;i++)
	{
    
    
		cin>>a>>b;
		to[a].push_back(b);
		to[b].push_back(a);
	}
	dfs(1,0);
	sort(h+1,h+1+n);
	cout<<unique(h+1,h+1+n)-h-1;
	return 0;
}

おすすめ

転載: blog.csdn.net/qq_52358098/article/details/132240662