天天看点

ZOJ 3602 Count the Trees

Description

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

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

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

Hint

The two trees in the first sample look like this.

References

  • ​​http://en.wikipedia.org/wiki/Binary_tree​​

判断一样的子树有多少对,用hash判断。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<ctime>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
int T, n[2], tot;
int t[2][maxn][2];
ll ans, cnt[maxn];
map<pair<int, int>, int> M;

int dfs(int x, int y)
{
  if (y == -1) return 0;
  int l = dfs(x, t[x][y][0]), r = dfs(x, t[x][y][1]);
  int k = M[make_pair(l, r)];
  if (!x)
  {
    if (!k) k = M[make_pair(l, r)] = ++tot;
    cnt[k]++;
    return k;
  }
  else
  {
    ans += cnt[k];
    return k ? k : -1;
  }
}

int main()
{
  scanf("%d", &T);
  while (T--)
  {
    scanf("%d%d", &n[0], &n[1]);
    M.clear();
    for (int i = 0; i < 2; i++)
    {
      for (int j = 1; j <= n[i]; j++)
      {
        cnt[j] = 0;
        for (int k = 0; k < 2; k++)
        {
          scanf("%d", &t[i][j][k]);
        }
      }
    }
    cnt[0] = tot = ans = 0;
    dfs(0, 1);
    dfs(1, 1);
    printf("%lld\n", ans);
  }
  return 0;
}      
#include<map>
#include<cmath>    
#include<queue>    
#include<vector>
#include<cstdio>    
#include<cstring>    
#include<algorithm>    
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))    
#define rep(i,j,k) for(int i=j;i<=k;i++)    
#define per(i,j,k) for(int i=j;i>=k;i--)    
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    
#define inone(x) scanf("%d",&x)    
#define intwo(x,y) scanf("%d%d",&x,&y)    
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int T, n, m, l[N], r[N], f[N], g[N], cnt;
map<pair<int, int>, int> M;
LL ans;

void dfs(int x)
{
  if (l[x]) dfs(l[x]);
  if (r[x]) dfs(r[x]);
  if (!M[make_pair(g[l[x]], g[r[x]])]) M[make_pair(g[l[x]], g[r[x]])] = ++cnt;
  f[g[x] = M[make_pair(g[l[x]], g[r[x]])]]++;
}

void Dfs(int x)
{
  if (l[x]) Dfs(l[x]);
  if (r[x]) Dfs(r[x]);
  if (!M[make_pair(g[l[x]], g[r[x]])]) M[make_pair(g[l[x]], g[r[x]])] = ++cnt;
  ans += f[g[x] = M[make_pair(g[l[x]], g[r[x]])]];
}

int main()
{
  for (inone(T); T--;)
  {
    intwo(n, m); g[0] = cnt = 0;
    rep(i, 1, n)
    {
      intwo(l[i], r[i]);
      l[i] = max(l[i], 0);
      r[i] = max(r[i], 0);
    }
    M.clear();  ms(f, 0);  dfs(1);
    rep(i, 1, m)
    {
      intwo(l[i], r[i]);
      l[i] = max(l[i], 0);
      r[i] = max(r[i], 0);
    }
    ans = 0; Dfs(1);
    printf("%lld\n", ans);
  }
  return 0;
}