http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602

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

Hint

The two trees in the first sample look like this.

References


Author: ZHUANG, Junyuan; WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

AC代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm> using namespace std; #define MAXN 100010 struct Edge {
int v, e, label;
Edge *link;
} edge[MAXN], *adj[MAXN]; int totE;
int f[MAXN], g[MAXN], val[MAXN], degree[MAXN], fa[MAXN];
int vec[], Q[MAXN];
int a = , b = , pp = , q = ; void addEdge(int u, int v, int label) {
Edge *p = &edge[totE++];
p->v = v;
p->label = label;
p->link = adj[u];
adj[u] = p;
} void cal(int n, int f[MAXN]) {
totE = ;
memset(adj, NULL, sizeof(adj));
memset(degree, , sizeof(degree));
for (int i = ; i <= n; ++i) {
int l, r;
scanf("%d%d", &l, &r);
if (l != -) {
addEdge(i, l, );
++degree[i];
fa[l] = i;
}
if (r != -) {
addEdge(i, r, );
++degree[i];
fa[r] = i;
}
}
int l = , r = ;
for (int i = ; i <= n; ++i) {
if (!degree[i])
Q[r++] = i;
}
while (l != r) {
int u = Q[l++];
Edge *p = adj[u];
int total = ;
while (p) {
vec[total++] = (long long) val[p->v] * p->label % q;
p = p->link;
}
val[u] = a;
for (int i = ; i < total; ++i) {
val[u] = (long long) val[u] * pp % q ^ vec[i] % q;
}
if(--degree[fa[u]] == ) Q[r++] = fa[u];
}
for (int i = ; i <= n; ++i)
f[i] = val[i];
sort(f + , f + + n);
} int main() {
int T;
scanf("%d", &T);
for (int cas = ; cas <= T; ++cas) {
int n, m;
scanf("%d%d", &n, &m);
cal(n, f);
cal(m, g);
int p1 = , p2 = ;
long long res = ;
while (p1 <= n && p2 <= m) {
if (f[p1] > g[p2])
++p2;
else if (f[p1] < g[p2])
++p1;
else {
int p3 = p1;
while (p3 + <= n && f[p3 + ] == f[p1])
++p3;
int p4 = p2;
while (p4 + <= m && g[p4 + ] == g[p2])
++p4;
res += (long long) (p3 - p1 + ) * (p4 - p2 + );
p1 = p3 + ;
p2 = p4 + ;
}
}
printf("%lld\n", res);
}
return ;
}

zjuoj 3602 Count the Trees的更多相关文章

  1. Count the Trees[HDU1131]

    Count the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)

     Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...

  3. 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 di ...

  4. HDU 1131 Count the Trees 大数计算

    题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...

  5. UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

    题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...

  6. uva 10007 Count the Trees

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  7. HDU 1131 Count the Trees

    卡特兰数再乘上n的阶乘 #include<iostream> #include<cstdio> using namespace std; #define base 10000 ...

  8. ZOJ3602:Count the Trees

    我是在neuqvj上交的这题:http://vj.acmclub.cn/problem/viewProblem.action?id=17848 本来是挺容易的树同构题,可是节点数比较多,愣是把普通ha ...

  9. 2012-2014 三年浙江 acm 省赛 题目 分类

    The 9th Zhejiang Provincial Collegiate Programming Contest A    Taxi Fare    25.57% (166/649)     (水 ...

随机推荐

  1. 10.28&29(NOIP模拟&修正&总结)

    三道题: T1:约数的约数的个数和.数论.但是简单暴力A了. T2:前k大的(带权点ai与带权点bi的和)的和.二分.骗40. T3:三维空间内每次减少有与空点的点,前后左右相连,求最短时间减为空.d ...

  2. c#的学习

    C#,读做 "C sharp",中文译音暂时没有,非专业人士一般读"C井",专业人士一般读"C sharp".C#是一种安全的.稳定的.简单 ...

  3. cocos2dx中设置横竖版

    IOS目录中RootViewController.mm//显示竖屏- (BOOL) shouldAutorotate { return NO;} //显示横屏- (BOOL) shouldAutoro ...

  4. python算法——第四天

    一.递归 def func(num): if num / 2 > 0: num -= 1 print(num) num = func(num) print('quit') return num ...

  5. 创建 maven 本地仓库

    在 pom.xml 添加依赖包的时候,有时候会提示无法从 http://repo1.maven.org/maven2/ 获取的情况,这时可配置个本地仓库: 从网上下载 maven 仓库网站源码包 Ne ...

  6. OpenCV 3.1 StereoBM 获取正确视差Dispariy

    OpenCV更新到3.0版本后,Stereo模块变化的挺多的,首先去掉了StereoBMState和StereoSGBMState这两个专门控制BM和SGBM算法参数的类,而且StereoBM不能直接 ...

  7. css形状大全

    转至:http://blog.sina.com.cn/s/blog_4abb9bba0101acsx.html

  8. 使用javascript实现在页面打印的效果的三种方式

    <div id="console"></div> <script type="text/javascript"> var c ...

  9. Xtrabackup2.3.4安装

    安装过程种有很多报错,这里我就不一一解释. wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.3.5/bin ...

  10. Layer弹窗组件

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. Layer的开发手册和下载地址 http://layer ...