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) (水 ...
随机推荐
- 【BZOJ2190】【SDOI2008】仪仗队
Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是 ...
- 【CodeVS】p1038 一元三次方程求解
题目描述 Description 有形如:ax3+bx2+cx+d=0 这样的一个一元三次方程.给出该方程中各项的系数(a,b,c,d 均为实数),并约定该方程存在三个不同实根(根的范围在-100 ...
- js的小效果-图片放大镜效果
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- Net-SNMP是线程安全的吗
原文地址 : http://www.net-snmp.org/wiki/index.php/FAQ:General_19 Net-SNMP是线程安全的吗? 确切的说,不是.不过呢,在多线程管理的应用进 ...
- 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- Android查询系统的音频(音乐播放器的核心)
//查询系统的音频库 public static List<MusicBean> getMusicInfo(Context context){ List<MusicBean> ...
- MySQL中引号的问题
原文: http://blog.csdn.net/wisgood/article/details/6317543 mysql中一个字符串,既可以用两个单引号表示,也可以用两个双引号表示. 比如字符串 ...
- ThinkPHP的URL优化
在刚刚安装好tp框架时,我们的路径一般是这样的: http://localhost/index.php/Home/Index/index?name=cly 我们可以通过下面一步步的步骤来优化这个路径 ...
- JSONObject简介
JSONObject简介 本节摘要:之前对JSON做了一次简单的介绍,并把JSON和XML做了一个简单的比较:那么,我就在想,如果是一个json格式的字符串传到后台,需要怎么对其处理?如果前台页面需要 ...