ZOJ-3602Count the Trees(hash处理+map)

Count the Trees

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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.

Input

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.

Output

For each test case, print a line containing the result.

Sample Input
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
Sample Output
1
11

题意:找两棵二叉树的同构子树有多少个.

思路:hash处理,每个节点的hash值代表以此为根节点的子树的hash值,用pair组成的数对存入map来标记这种子树,这样每种子树的hash值都是唯一的,叶子节点的hash值均为0.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 4e5+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
map<int,int>::iterator it;
struct node
{
	int l,r,f;
} t[2][maxn];

ll ans;
int n[2],cnt;
int d[2][maxn];
int hash[2][maxn];//第i棵树以第j个节点的根的子树的hash值 
int sum[2][maxn];//第i棵树hash值为j的子树的个数 
map<pair<int,int>,int> mp;

void solve(int i)
{
	queue<int> q;
	for(int j = 1;j<= n[i];j++)
	{
		if(d[i][j] == 0)
		{
			hash[i][j] = 0;
			q.push(j);
		}
	}
	
	while(!q.empty())
	{
		int now = q.front();
		q.pop();
		
		int L = -1,R = -1;
		if(t[i][now].l != -1)
			L = hash[i][t[i][now].l];
		if(t[i][now].r != -1)
			R = hash[i][t[i][now].r];
		pair<int,int> tmp = make_pair(L,R);
		
		if(mp[tmp] == 0)
			mp[tmp] = ++cnt;
		sum[i][mp[tmp]]++;
		hash[i][now] = mp[tmp];
		if(i == 1)
			ans+= sum[0][mp[tmp]];
		
		if(--d[i][t[i][now].f] == 0)
			q.push(t[i][now].f);
	}
}

void init()
{
	ans = 0;
	cnt = 0;
	mp.clear();
	mem(sum,0);
	mem(d,0);
}

int main()
{
	int tt;
	cin>>tt;
	
	while(tt--)
	{
		init();
		cin>>n[0]>>n[1];
		for(int j = 0;j< 2;j++)
		{
			for(int i = 1;i<= n[j];i++)
			{
				scanf("%d %d",&t[j][i].l,&t[j][i].r);
				if(t[j][i].l!= -1)
					t[j][t[j][i].l].f = i,d[j][i]++;
				if(t[j][i].r!= -1)
					t[j][t[j][i].r].f = i,d[j][i]++;
			}	
		}
		solve(0);
		solve(1);
		cout<<ans<<endl;
	}	
	
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值