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发现,两次画的顺序是不一样的,这就导致了原本的某一个节点 ...
随机推荐
- Spring中AOP的实现
Spring中整合了AOP的功能,虽然有不足,没有专门做AOP框架的那么完美,但是用一用感觉还是不错的 一些概念: AOP 面向切面编程 aspect 切面/切面类(我个人认为一个真正被解耦的程序,切 ...
- Shuffle过程详解
- python+selenium中webdriver相关资源
Chrome chrome的webdriver : http://chromedriver.storage.googleapis.com/index.html chrome的webdriver需要对 ...
- csscomb配置文件说明
{ "always-semicolon": true, // 总是显示分号 "block-indent": " ", // 代码块缩进,可以 ...
- 数论,质因数,gcd——cf1033D 好题!
直接筛质数肯定是不行的 用map<ll,ll>来保存质因子的指数 考虑只有3-5个因子的数的组成情况 必定是a=pq or a=p*p or a=p*p*p or a=p*p*p*p 先用 ...
- Ubuntu下使用SSH 命令用于登录远程桌面
https://blog.csdn.net/yucicheung/article/details/79427578 问题描述 做DL的经常需要在一台电脑(本地主机)上写代码,另一台电脑(服务器,计算力 ...
- IO流读取和写入文件
package com.xmlmysql.demo.config; import java.io.BufferedReader; import java.io.BufferedWriter; impo ...
- axios请求头几种区别:application/x-www-form-urlencoded
今天小伙伴问我们项目axios默认请求头是application/x-www-form-urlencoded;charset=UTF-8, 现在有个后端接口要求请求头方式为application/js ...
- privoxy 安装
https://www.privoxy.org/sf-download-mirror/Sources/ 1.挑选源码版本,下载,解压 2.增加用户 useradd privoxy 3.make &am ...
- 《DSP using MATLAB》Problem 8.26
代码: %% ------------------------------------------------------------------------ %% Output Info about ...