Similarity of Subtrees(深搜+hash、映射)

来源:互联网 发布:网络维修培训 编辑:程序博客网 时间:2024/05/21 06:57

https://vjudge.net/problem/Aizu-2784

看http://www.cnblogs.com/chen9510/p/5929542.html
hash方法,因为每一个一样的node都可以变为一个同样大小的多项式的。。。

map<ULL,LL>mp;vector<int>g[N];ULL dp[N];LL ans;int n;const ULL b=1000000007;void dfs(int u,int fa){    dp[u]=1;    for(int i=0;i<g[u].size();++i){        int v=g[u][i];        if(v==fa)continue;        dfs(v,u);        dp[u]+=dp[v]*b;    }    ans+=mp[dp[u]];    mp[dp[u]]++;}int main(){    sf("%d",&n);        ans=0;        rep(i,1,n-1){            int u,v;sf("%d%d",&u,&v);            g[u].push_back(v);g[v].push_back(u);        }        dfs(1,1);        cout<<ans<<'\n';}
原创粉丝点击