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代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 #define MAXN 100010
 8 
 9 struct Edge {
10     int v, e, label;
11     Edge *link;
12 } edge[MAXN], *adj[MAXN];
13 
14 int totE;
15 int f[MAXN], g[MAXN], val[MAXN], degree[MAXN], fa[MAXN];
16 int vec[2], Q[MAXN];
17 int a = 13, b = 7, pp = 11, q = 1000000007;
18 
19 void addEdge(int u, int v, int label) {
20     Edge *p = &edge[totE++];
21     p->v = v;
22     p->label = label;
23     p->link = adj[u];
24     adj[u] = p;
25 }
26 
27 void cal(int n, int f[MAXN]) {
28     totE = 0;
29     memset(adj, NULL, sizeof(adj));
30     memset(degree, 0, sizeof(degree));
31     for (int i = 1; i <= n; ++i) {
32         int l, r;
33         scanf("%d%d", &l, &r);
34         if (l != -1) {
35             addEdge(i, l, 137);
36             ++degree[i];
37             fa[l] = i;
38         }
39         if (r != -1) {
40             addEdge(i, r, 1007);
41             ++degree[i];
42             fa[r] = i;
43         }
44     }
45     int l = 0, r = 0;
46     for (int i = 1; i <= n; ++i) {
47         if (!degree[i])
48             Q[r++] = i;
49     }
50     while (l != r) {
51         int u = Q[l++];
52         Edge *p = adj[u];
53         int total = 0;
54         while (p) {
55             vec[total++] = (long long) val[p->v] * p->label % q;
56             p = p->link;
57         }
58         val[u] = a;
59         for (int i = 0; i < total; ++i) {
60             val[u] = (long long) val[u] * pp % q ^ vec[i] % q;
61         }
62         if(--degree[fa[u]] == 0) Q[r++] = fa[u];
63     }
64     for (int i = 1; i <= n; ++i)
65         f[i] = val[i];
66     sort(f + 1, f + 1 + n);
67 }
68 
69 int main() {
70     int T;
71     scanf("%d", &T);
72     for (int cas = 1; cas <= T; ++cas) {
73         int n, m;
74         scanf("%d%d", &n, &m);
75         cal(n, f);
76         cal(m, g);
77         int p1 = 1, p2 = 1;
78         long long res = 0;
79         while (p1 <= n && p2 <= m) {
80             if (f[p1] > g[p2])
81                 ++p2;
82             else if (f[p1] < g[p2])
83                 ++p1;
84             else {
85                 int p3 = p1;
86                 while (p3 + 1 <= n && f[p3 + 1] == f[p1])
87                     ++p3;
88                 int p4 = p2;
89                 while (p4 + 1 <= m && g[p4 + 1] == g[p2])
90                     ++p4;
91                 res += (long long) (p3 - p1 + 1) * (p4 - p2 + 1);
92                 p1 = p3 + 1;
93                 p2 = p4 + 1;
94             }
95         }
96         printf("%lld
", res);
97     }
98     return 0;
99 }
View Code
原文地址:https://www.cnblogs.com/jeff-wgc/p/4472605.html