您的位置:首页 > 其它

zjuoj 3602 Count the Trees

2015-05-02 22:15 411 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602

Count the TreesTime 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.

#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[2], Q[MAXN];
int a = 13, b = 7, pp = 11, q = 1000000007;

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 = 0;
memset(adj, NULL, sizeof(adj));
memset(degree, 0, sizeof(degree));
for (int i = 1; i <= n; ++i) {
int l, r;
scanf("%d%d", &l, &r);
if (l != -1) {
addEdge(i, l, 137);
++degree[i];
fa[l] = i;
}
if (r != -1) {
addEdge(i, r, 1007);
++degree[i];
fa[r] = i;
}
}
int l = 0, r = 0;
for (int i = 1; i <= n; ++i) {
if (!degree[i])
Q[r++] = i;
}
while (l != r) {
int u = Q[l++];
Edge *p = adj[u];
int total = 0;
while (p) {
vec[total++] = (long long) val[p->v] * p->label % q;
p = p->link;
}
val[u] = a;
for (int i = 0; i < total; ++i) {
val[u] = (long long) val[u] * pp % q ^ vec[i] % q;
}
if(--degree[fa[u]] == 0) Q[r++] = fa[u];
}
for (int i = 1; i <= n; ++i)
f[i] = val[i];
sort(f + 1, f + 1 + n);
}

int main() {
int T;
scanf("%d", &T);
for (int cas = 1; cas <= T; ++cas) {
int n, m;
scanf("%d%d", &n, &m);
cal(n, f);
cal(m, g);
int p1 = 1, p2 = 1;
long long res = 0;
while (p1 <= n && p2 <= m) {
if (f[p1] > g[p2])
++p2;
else if (f[p1] < g[p2])
++p1;
else {
int p3 = p1;
while (p3 + 1 <= n && f[p3 + 1] == f[p1])
++p3;
int p4 = p2;
while (p4 + 1 <= m && g[p4 + 1] == g[p2])
++p4;
res += (long long) (p3 - p1 + 1) * (p4 - p2 + 1);
p1 = p3 + 1;
p2 = p4 + 1;
}
}
printf("%lld\n", res);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: