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) (水 ...
随机推荐
- 51nod算法马拉松14
这次太丢人了只搞出来了A到D,那就将就写一写A到D... A 棋盘问题 脑筋急转弯题,不难发现每一次两个人只能染白奇数个格子,所以数数有奇数还是偶数个白格子就行了. #include<cstdi ...
- minicom使用
http://blog.chinaunix.net/uid-9525959-id-2001815.html 创建log文件 : minicom -C my_capturefile
- 改进iOS客户端的升级提醒功能
改进iOS客户端的升级提醒功能 功能设计 先申明一下,我是码农,不是一个产品经理,但我觉得现有市面上的很多 App,设计的 "升级提示功能" 都不太友好.在此分享一下我的想法,欢迎 ...
- Convert between cv::Mat and QImage 两种图片类转换
在使用Qt和OpenCV混合编程时,我们有时需要在两种图片类cv::Mat和QImage之间进行转换,下面的代码参考了网上这个帖子: //##### cv::Mat ---> QImage ## ...
- Android自定义UI模板
第一步:自定义xml属性 新建一个android项目,在values文件夹中新建一个atts.xml的文件,在这个xml文件中声明我们一会在使用自定义控件时候需要指明的属性.atts.xml < ...
- Java读取Execl表格数据
在前面提到用java代码新建一个Execl 表格并添加数据到表格中, 这次写了一个读取Execl表格数据并添加导数据库中的案列 给定对方一个Execl模板表格,如果导入的Execl表格和预订的表格不相 ...
- 状态压缩DP
K - Necklace Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:327680KB ...
- Oracle加密表空间进行数据加密的示例
接上篇:http://www.cnblogs.com/myrunning/p/4292049.html 1查看数据库版本 2查看当前数据库表空间 从这里看到我们此时数据库里没有加密表空间. 3创建加密 ...
- SQL,LINQ,Lambda语法对照图(转载)
如果你熟悉SQL语句,当使用LINQ时,会有似曾相识的感觉.但又略有不同.下面是SQL和LINQ,Lambda语法对照图 SQL LINQ Lambda SELECT * FROM HumanReso ...
- Linux常用文件介绍
您的一举一动都会记录在/var/log/secure 和/var/log/messages文件中