描述

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值,第二遍计算。

代码

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+;
ll ans;
int a[][N],num[N],tot,f;
map< pair<int,int>,int >ma;
int dfs(int u)
{
int ls=-,rs=-,t;
if(a[][u]!=-)ls=dfs(a[][u]);
if(a[][u]!=-)rs=dfs(a[][u]);
if(f)
{
if(ma.count({ls,rs}))ans+=num[t=ma[{ls,rs}]];
else t=;//这里没加wa了一次
}
else
{
if(!ma.count({ls,rs}))ma[{ls,rs}]=++tot;
num[t=ma[{ls,rs}]]++;
}
return t;
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
ma.clear();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)num[i]=;
for(int i=;i<=n;i++)scanf("%d%d",&a[][i],&a[][i]);
tot=;f=;dfs();
for(int i=;i<=m;i++)scanf("%d%d",&a[][i],&a[][i]);
ans=;f=;dfs();
printf("%lld\n",ans);
}
return ;
}

TZOJ 4292 Count the Trees(树hash)的更多相关文章

  1. zjuoj 3602 Count the Trees

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...

  2. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  3. Count the Trees[HDU1131]

    Count the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)

     Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...

  5. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  6. BZOJ_2124_等差子序列_线段树+Hash

    BZOJ_2124_等差子序列_线段树+Hash Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pL ...

  7. bzoj2124: 等差子序列线段树+hash

    bzoj2124: 等差子序列线段树+hash 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2124 思路 找大于3的等差数列其实就是找等于 ...

  8. BZOJ4337:[BJOI2015]树的同构(树hash)

    Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...

  9. 【CSP模拟赛】仔细的检查(树的重心&树hash)

    题目描述 nodgd家里种了一棵树,有一天nodgd比较无聊,就把这棵树画在了一张纸上.另一天nodgd更无聊,就又画了一张.  这时nodgd发现,两次画的顺序是不一样的,这就导致了原本的某一个节点 ...

随机推荐

  1. java中一个类不想被继承怎么办?

    方法一:把类声明为final 方法二:对类中的构造器声明为private,类中提供一个static方法,完成对类的初始化.如下代码: public class Base{ private Base() ...

  2. java-day07

    API 应用程序编程接口 Scanner类 将键盘输入的数据到程序中 1.导包 2.创建 Scanner 对象名 = new Scanner(); 3.使用 int num = 对象名.nextInt ...

  3. Win10命令提示符git log中文乱码的解决方案

    在系统环境变量中新建一个名为LESSCHARSET的变量 其值为utf-8 新建完毕后应用,git log就不会出现乱码的问题了^_^ 参考博文:git- win10 cmd git log 中文乱码 ...

  4. JAVA中日期 yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别

    HH是24小时制,hh是12小时制 区别就是:大写的H是二十四小时制的小时数(0-23),小写的h是十二小时制的小时数(am/pm 1-12) //24小时制 SimpleDateFormat sdf ...

  5. wpf Rectangle

    <Rectagle Width="100" Height="100" Stroke="Black" Fill="Blue&q ...

  6. 【转载】Kafka介绍及升级经验分享

    http://blog.talkingdata.net/?p=3165 背景 当时的现状:开始使用Kafka的时候,使用的版本是0.7.2,当时的目的是为了替代kestrel,主要是使用Kafka来做 ...

  7. jquery 表单验证插件

    其他: <form action=""> First name: <input type="text" name="FirstNam ...

  8. nodejs的安装和环境配置

    在安装npm的时候可能会报错, 可以按准过cnpm时淘宝在国内的npm镜像 命令如下:npm install -g cnpm --registry=https://registry.npm.taoba ...

  9. curl http_code 状态码

    1.只得到一个url的http_code的状态码 curl -I -m 10 -o /dev/null -s -w %{http_code} http://127.0.0.1/a.html 查询hea ...

  10. leetcode-92-反转链表②

    题目描述: 方法一: class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: ...