Zoj 3602 count the homogeneity (hash) of the trees tree)

Source: Internet
Author: User

Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4705

Question: Give Two Binary Trees A and B, and ask how many pairs have the same structure as the sub-tree a in a and the sub-tree B in B.

Idea: hash. Different numbers correspond to different structures. For example, 1 represents a separate leaf node, 2 indicates that the Left subtree is a leaf node without the right subtree... every time a new subtree occurs, it is recorded, the record method is to use the DFS backtracking process to determine whether the left subtree and the right subtree have appeared (the pair is used to record the subtree, that is, the left and right subtree, two maps. The first map indicates the paing between the pair of the subtree and its labels. The second map indicates the number of occurrences of each label ).

Code:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 100005#define PI acos(-1.0)#define seed 31//131,1313typedef long long LL;typedef unsigned long long ULL;using namespace std;int top=0;map < pair < int , int > , int > M;map < int , LL > N ;int tree_a[maxn][2],tree_b[maxn][2],a[maxn],b[maxn];LL ans;void dfs_a(int u){    pair < int , int > p;    int left,right;    left=tree_a[u][0],right=tree_a[u][1];    if(left!=-1)        dfs_a(left);    if(right!=-1)        dfs_a(right);    if(left==-1)        p.first=-1;    else p.first=a[left];    if(right==-1)        p.second=-1;    else p.second=a[right];    if(M.find(p)==M.end())    {        M[p]=top;        a[u]=top;        N[top]=1;        top+=3;    }    else    {        a[u]=M[p];        N[M[p]]++;    }}void dfs_b(int u){    pair <int ,int > p ;    int left=tree_b[u][0],right=tree_b[u][1];    if(left!=-1)        dfs_b(left);    if(right!=-1)        dfs_b(right);    if(left==-1)        p.first=-1;    else p.first=b[left];    if(right==-1)        p.second=-1;    else p.second=b[right];    if(M.find(p)!=M.end())    {        ans+=N[M[p]];        b[u]=M[p];    }}void init(){    top=0;    ans=0;    N.clear();    M.clear();    for(int i=0;i<=100000;i++)        a[i]=-2,b[i]=-2;}int main(){    int T,n,m;    scanf("%d",&T);    while(T--)    {        init();        scanf("%d%d",&n,&m);        for(int i=1; i<=n; i++)            scanf("%d%d",&tree_a[i][0],&tree_a[i][1]);        for(int i=1; i<=m; i++)            scanf("%d%d",&tree_b[i][0],&tree_b[i][1]);        dfs_a(1);        dfs_b(1);        printf("%lld\n",ans);    }    return 0;}


Zoj 3602 count the homogeneity (hash) of the trees 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.