ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TZOJ 4292 Count the Trees(树hash)

2019-10-11 09:54:30  阅读:276  来源: 互联网

标签:Count hash rs int ma tree trees ls TZOJ


描述

A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T. Two binary trees are called identical if their left subtrees are the same(or both having no left subtree) and their right subtrees are the same(or both having no right subtrees).

According to a recent research, some people in the world are interested in counting the number of identical subtree pairs, each from the given trees respectively.

Now, you are given two trees. Write a program to help to count the number of identical subtree pairs, such that the first one comes from the first tree and the second one comes from the second tree.

输入

There are multiple test cases. The first line contains a positive integer T (T ≤ 20) indicating the number of test cases. Then T test cases follow.

In each test case, There are two integers n and m (1 ≤ n, m ≤ 100000) indicating the number of nodes in the given two trees. The following n lines describe the first tree. The i-th line contains two integers u and v (1 ≤ u ≤ n or u = -1, 1 ≤ v ≤ n or v = -1) indicating the indices of the left and right children of node i. If u or v equals to -1, it means that node i don't have the corresponding left or right child. Then followed by m lines describing the second tree in the same format. The roots of both trees are node 1.

输出

For each test case, print a line containing the result.

样例输入

2
2 2
-1 2
-1 -1
2 -1
-1 -1
5 5
2 3
4 5
-1 -1
-1 -1
-1 -1
2 3
4 5
-1 -1
-1 -1
-1 -1

样例输出

1
11

提示

The two trees in the first sample look like this.

题意

给你两棵二叉树,问有多少颗子树完全相同。

题解

树hash,一颗二叉子树的hash值等于pair(左儿子,右儿子)的hash值。

那么进行两遍dfs,第一遍统计hash值,第二遍计算。

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int N=1e5+5;
 5 ll ans;
 6 int a[2][N],num[N],tot,f;
 7 map< pair<int,int>,int >ma;
 8 int dfs(int u)
 9 {
10     int ls=-1,rs=-1,t;
11     if(a[0][u]!=-1)ls=dfs(a[0][u]);
12     if(a[1][u]!=-1)rs=dfs(a[1][u]);
13     if(f)
14     {
15         if(ma.count({ls,rs}))ans+=num[t=ma[{ls,rs}]];
16     }
17     else
18     {
19         if(!ma.count({ls,rs}))ma[{ls,rs}]=++tot;
20         num[t=ma[{ls,rs}]]++;
21     }
22     return t;
23 }
24 int main()
25 {
26     int t,n,m;
27     scanf("%d",&t);
28     while(t--)
29     {
30         scanf("%d%d",&n,&m);
31         for(int i=1;i<=n;i++)num[i]=0;
32         for(int i=1;i<=n;i++)scanf("%d%d",&a[0][i],&a[1][i]);
33         tot=0;f=0;dfs(1);
34         for(int i=1;i<=m;i++)scanf("%d%d",&a[0][i],&a[1][i]);
35         ans=0;f=1;dfs(1);ma.clear();
36         printf("%lld\n",ans);
37     }
38     return 0;
39 }

标签:Count,hash,rs,int,ma,tree,trees,ls,TZOJ
来源: https://www.cnblogs.com/taozi1115402474/p/11652192.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有