描述

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. easyui combotree的使用示例

    一.View: 1.定义输入控件 <input id="ParentId" name="ParentId"> 2.绑定combotree $('#P ...

  2. MapReduce各个执行阶段

  3. Python全栈开发:socket

    Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...

  4. 新安装一个eclipse,导入一个web项目,右键点击项目选择Properties,找不到project facets和Server选项。

    解决方式: 1.点击:eclipse导航栏中点击Help->Install New Software 2.点击Add添加 3在弹出框中填写以下信息 name:keep(名字随便取) locati ...

  5. windows pip 安装 转载

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  6. STL vector容器需要警惕的一些坑

    从迭代器中取值切记需要判断是否为空 例如: vector<int> vtTest; vtTest.clear(); if (vtTest.empty()){ ; } ]; 如果没有忘了判断 ...

  7. Ubuntu GitHub操作——创建仓库

    一.创建仓库 1.初始化仓库(只在初始化时执行一次) git init 这部是新建并初始化仓库 2.向仓库中添加文件 git add . 将文件夹内的所有文件都添加到仓库中(注意小“.”),在这部之后 ...

  8. 第十二章 Odoo 12开发之报表和服务端 QWeb

    报表是业务应用非常有价值的功能,内置的 QWeb 引擎是报表的默认引擎.使用 QWeb 模板设计的报表可生成 HTML 文件并被转化成 PDF.也就是说我们可以很便捷地利用已学习的 QWeb 知识,应 ...

  9. WCF服务编程-基础

    WCF是微软建立新一代的分布式应用及面向服务应用的标准平台,是基于原有.NET Framework 2.0的扩展.虽然在WCF发布不久就已经在项目中使用WCF技术了.但是由于在项目中还没有较大规模的应 ...

  10. 报错C1189 #error: "No Target Architecture"

    根本原因: 是因为单独包含了一些windows.h已经包含了的头文件如"fileapi.h","WinUser.h",但是却没有包含windows.h 或者先包 ...