TZOJ 4292 Count the Trees(树hash)
描述
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)的更多相关文章
- zjuoj 3602 Count the Trees
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...
- Count Colour_poj2777(线段树+位)
POJ 2777 Count Color (线段树) Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Count the Trees[HDU1131]
Count the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
Count the Trees Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...
- 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 ...
- BZOJ_2124_等差子序列_线段树+Hash
BZOJ_2124_等差子序列_线段树+Hash Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pL ...
- bzoj2124: 等差子序列线段树+hash
bzoj2124: 等差子序列线段树+hash 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2124 思路 找大于3的等差数列其实就是找等于 ...
- BZOJ4337:[BJOI2015]树的同构(树hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- 【CSP模拟赛】仔细的检查(树的重心&树hash)
题目描述 nodgd家里种了一棵树,有一天nodgd比较无聊,就把这棵树画在了一张纸上.另一天nodgd更无聊,就又画了一张. 这时nodgd发现,两次画的顺序是不一样的,这就导致了原本的某一个节点 ...
随机推荐
- 【SDOI2015】约数个数和
题面 求\(\sum_{i=1}^n\sum_{j=1}^md(ij)\) \(\leq 50000\)组数据,\(1\leq n,m\leq 50000\). 题目分析 首先,你需要知道一个结论: ...
- input 数值验证
1.手动校验数字为整数 Number.isInteger <el-input class="radioInput" v-model.number="ruleForm ...
- LUOGU P4281 [AHOI2008]紧急集合 / 聚会 (lca)
传送门 解题思路 可以通过手玩或打表发现,其实要选的点一定是他们三个两两配对后其中一对的$lca$上,那么就直接算出来所有的$lca$,比较大小就行了. #include<iostream> ...
- 19-11-14-Finally
如果这是世界末日的前一晚, 这是我的回答. #include <bits/stdc++.h> using namespace std; int main(){ cout<<&q ...
- Mac 下搭建vue开发环境
tips:一定要有翻墙工具如lanter,另外要保证网速OK. 1. 首先需要安装homebrew liukingdeMBP:~ liuking$ /usr/bin/ruby -e "$(c ...
- 杂项-VOD:VOD(视频点播)
ylbtech-杂项-VOD:VOD(视频点播) 视频点播是二十世纪90年代在国外发展起来的,英文称为“Video on Demand”,所以也称为“VOD”.顾名思义,就是根据观众的要求播放节目的视 ...
- python用reduce和map把字符串转为数字的方法
python用reduce和map把字符串转为数字的方法 最近在复习高阶函数的时候,有一道题想了半天解不出来.于是上午搜索资料,看了下别人的解法,发现学习编程,思维真的很重要.下面这篇文章就来给大家介 ...
- Git命令使用和配置
git config --global user.name "your name" git config --global user.email "your email& ...
- java基础之BigInteger
BigInteger类概述可以让超过Integer范围内的数据进行运算 构造方法 public BigInteger(String val) 成员方法: public BigInteger add(B ...
- C开发系列-include
include 在include目录下有两个文件,分别为main.m, abc.txt main.m内容 #include <stdio.h> int main(){ #include & ...