ZOJ--3602 -- count the homogeneity of the trees [DFS + hash] tree

Source: Internet
Author: User

Link:Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 3602.

Question:A binary tree with N nodes and a binary tree with M nodes are provided, and information about the left and right Subtrees of each node is provided. The two trees have several identical Subtrees.


Ideas:Tree homogeneity, no idea during the competition, other people's problem-solving reports read after the competition. In fact, it is a hash value for the left and right Subtrees of each node. It does not need to be as troublesome as a string hash. Just mark each subtree with a number, map the left and right subtree information of each node to a tag value. Use DFS to assign a hash value to each node of the two trees. After the assignment, determine the hash values of each node in the first tree and each node in the second tree. If the values are equal, the answer is + 1, but the number of nodes is too large, in this way, the traversal time complexity is 100000*100000. It uses a new map to record all the hash values of the first number, in the second tree, we can determine that if the second tree node has a ing in this map, the answer will add the corresponding value of this ing, so that the complexity is 2*100000, not TLE.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 10100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{    int u,v;}a[101000],b[101000];int n,m,cnt;int ans1[101000],ans2[101000];map<pair<int,int>,int>mp;void dfs(int u,node c[],int ans[]){    if(c[u].u==-1)  ans[c[u].u] = -1;    else    dfs(c[u].u,c,ans);    if(c[u].v==-1)  ans[c[u].v] = -1;    else    dfs(c[u].v,c,ans);    pair<int,int> p = make_pair(ans[c[u].u],ans[c[u].v]);    if(mp.find(p)!=mp.end()) ans[u] = mp[p];    else{        mp[p] = cnt++;        ans[u] = mp[p];    }}int main(){    int t,i,j,u,v;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&m);        cnt = 0;        mp.clear();        for(i=1;i<=n;i++){            scanf("%d%d",&a[i].u,&a[i].v);        }        for(i=1;i<=m;i++){            scanf("%d%d",&b[i].u,&b[i].v);        }        dfs(1,a,ans1);        dfs(1,b,ans2);        map<int,int>fuck;        ll ans = 0;        for(i=1;i<=n;i++){            fuck[ans1[i]]++;        }        for(i=1;i<=m;i++){            ans += fuck[ans2[i]];        }        printf("%lld\n",ans);    }    return 0;}


ZOJ--3602 -- count the homogeneity of the trees [DFS + hash] tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.