描述

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. Win10命令提示符git log中文乱码的解决方案

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

  2. [WPF自定义控件]?Window(窗体)的UI元素及行为

    原文:[WPF自定义控件]?Window(窗体)的UI元素及行为 1. 前言 本来打算写一篇<自定义Window>的文章,但写着写着发觉内容太多,所以还是把使用WindowChrome自定 ...

  3. 2019-5-21-Total-Commander-显示文件包含文件名扩展

    title author date CreateTime categories Total Commander 显示文件包含文件名扩展 lindexi 2019-5-21 11:37:6 +0800 ...

  4. 单体内置对象——Global对象

    单体内置对象的定义:由ECMAScript实现提供的.不依赖于宿主环境的对象,这些对象在ECMAScript程序执行之前已经存在了.意思就是说:开发人员不必显式地实例化内置对象,因为他们已经实例化了. ...

  5. Android开发 LevelListDrawable详解

    前言 此篇博客正在施工中... 作者其实就是想挖个坑备忘一下... 十分抱歉, 可以参考https://www.jianshu.com/p/f9ec65241b6b

  6. iOS开发NSFetchedResultsController的使用CoreData和TableView数据同步更新

    1.效果 2.代码 #import "ViewController.h" #import "Student+CoreDataProperties.h" #def ...

  7. mui.fire()用法

    作用: mui.fire() 可以触发目标窗口的自定义事件 mui.fire( 目标窗口的webview , '自定义事件名' ,{参数列表}:) 目标窗口监听这个自定义事件 window.addEv ...

  8. GCC 参数详解

    转载出处:http://blog.csdn.net/yff1030/article/details/8592077 原文:http://www.cppblog.com/SEMAN/archive/20 ...

  9. 深入理解JVM(一)类加载器部分:双亲委派模型

    类加载器的父亲委托机制 在父亲委托机制中,各个类加载器按照父子关系形成了树形结构,除了根类加载器之外,其余的类加载器都有且只有一个父加载器. 先让最顶层可以加在的父加载器加栽(所有可加载的加载器中,处 ...

  10. 操作系统-CPU调度

    概念 控制.协调多个进程对CPU的竞争 即按一定的调度算法从就绪队列中选择一个进程,把CPU的使用权交给被选中的进程 场景 N个进程就绪,等待上M(M>=1)个CPU运行,需要决策哪个进程分配给 ...