zjuoj 3602 Count the Trees
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的更多相关文章
- 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) ...
- 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 ...
- HDU 1131 Count the Trees 大数计算
题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...
- UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)
题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...
- uva 10007 Count the Trees
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- HDU 1131 Count the Trees
卡特兰数再乘上n的阶乘 #include<iostream> #include<cstdio> using namespace std; #define base 10000 ...
- ZOJ3602:Count the Trees
我是在neuqvj上交的这题:http://vj.acmclub.cn/problem/viewProblem.action?id=17848 本来是挺容易的树同构题,可是节点数比较多,愣是把普通ha ...
- 2012-2014 三年浙江 acm 省赛 题目 分类
The 9th Zhejiang Provincial Collegiate Programming Contest A Taxi Fare 25.57% (166/649) (水 ...
随机推荐
- InterBase数据库迁移到MySQL(恢复备份)
我拿到的是InterBase导出的“.gbk”后缀的数据库备份文件,目标是可以通过命令行的方式导入到指定的数据库中,在这个脚本中我使用了InterBase数据库中自带的“gbak”命令行来进行操作. ...
- 定义 iOS 方法名等不错的规范
1.配置视图不应命名为 setxxxx, 而应叫做 showxxxx 2.让按钮高亮不应叫做 showxxx, 而应叫做 highlightedxxx. 3,弹出 toastView 可以用 show ...
- preventDefault()方法
该方法将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作). 例如,如果 type 属性是 "submit",在事件传播的任意阶段可以调用任意的事件句柄,通过调 ...
- 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- Struts2下的<result>中的type整理
1.<result name="处理结果名" type="相应结果类型">“响应内容”</result> 解释:name的值shift指 ...
- JQ写法和js写法 方法函数化
<script> $(function () { $('#head').click=function () { alert($(this).html()) } }) </script ...
- Java面试题大全(三)
81.如何设定的weblogic的热启动模式(开发模式)与产品发布模式? 可以在管理控制台中修改对应服务器的启动模式为开发或产品模式之一.或者修改服务的启动文件或者commenv文件,增加set PR ...
- 满足要求的最长上升子序列(nlogn)
题意:数列A1,A2,...,AN,修改最少的数字,使得数列严格单调递增.(1<=N<=10^5; 1<=Ai<=10^9 ) 思路:首先要明白的一点是数列是严格单调递增,那么 ...
- db2数据库新手可能碰到的问题及详解(部分内容来自网络搜索)
一.db2安装好之后出现乱码,菜单栏呈现方框状,此时选择菜单第五项,点击选择下拉菜单中的最后一项,打开选择标签卡的第三项(字体),如果是无衬线都改为有衬线,如果是有衬线改为无衬线.乱码即可解决(网上一 ...
- MySQL数据恢复和复制对InnoDB锁机制的影响
MySQL通过BINLOG记录执行成功的INSERT,UPDATE,DELETE等DML语句.并由此实现数据库的恢复(point-in-time)和复制(其原理与恢复类似,通过复制和执行二进制日志使一 ...