ZOJ 3602 Count the Trees
2016-04-19 22:11
489 查看
Description
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
Sample Output
![](http://7xjob4.com1.z0.glb.clouddn.com/a652cd86564f75d96aa3eb92f83a0df0)
判断一样的子树有多少对,用hash判断。
温故而知新#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int T, n, m, l
, r
, f
, g
, cnt;
map<pair<int, int>, int> M;
LL ans;
void dfs(int x)
{
if (l[x]) dfs(l[x]);
if (r[x]) dfs(r[x]);
if (!M[make_pair(g[l[x]], g[r[x]])]) M[make_pair(g[l[x]], g[r[x]])] = ++cnt;
f[g[x] = M[make_pair(g[l[x]], g[r[x]])]]++;
}
void Dfs(int x)
{
if (l[x]) Dfs(l[x]);
if (r[x]) Dfs(r[x]);
if (!M[make_pair(g[l[x]], g[r[x]])]) M[make_pair(g[l[x]], g[r[x]])] = ++cnt;
ans += f[g[x] = M[make_pair(g[l[x]], g[r[x]])]];
}
int main()
{
for (inone(T); T--;)
{
intwo(n, m); g[0] = cnt = 0;
rep(i, 1, n)
{
intwo(l[i], r[i]);
l[i] = max(l[i], 0);
r[i] = max(r[i], 0);
}
M.clear(); ms(f, 0); dfs(1);
rep(i, 1, m)
{
intwo(l[i], r[i]);
l[i] = max(l[i], 0);
r[i] = max(r[i], 0);
}
ans = 0; Dfs(1);
printf("%lld\n", ans);
}
return 0;
}
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
http://en.wikipedia.org/wiki/Binary_tree判断一样的子树有多少对,用hash判断。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<vector> #include<cmath> #include<algorithm> #include<map> #define ll long long using namespace std; const int mod = 1e9 + 7; const int maxn = 1e5 + 10; int T, n[2], tot; int t[2][maxn][2]; ll ans, cnt[maxn]; map<pair<int, int>, int> M; int dfs(int x, int y) { if (y == -1) return 0; int l = dfs(x, t[x][y][0]), r = dfs(x, t[x][y][1]); int k = M[make_pair(l, r)]; if (!x) { if (!k) k = M[make_pair(l, r)] = ++tot; cnt[k]++; return k; } else { ans += cnt[k]; return k ? k : -1; } } int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n[0], &n[1]); M.clear(); for (int i = 0; i < 2; i++) { for (int j = 1; j <= n[i]; j++) { cnt[j] = 0; for (int k = 0; k < 2; k++) { scanf("%d", &t[i][j][k]); } } } cnt[0] = tot = ans = 0; dfs(0, 1); dfs(1, 1); printf("%lld\n", ans); } return 0; }
温故而知新#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)
typedef long long LL;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
int T, n, m, l
, r
, f
, g
, cnt;
map<pair<int, int>, int> M;
LL ans;
void dfs(int x)
{
if (l[x]) dfs(l[x]);
if (r[x]) dfs(r[x]);
if (!M[make_pair(g[l[x]], g[r[x]])]) M[make_pair(g[l[x]], g[r[x]])] = ++cnt;
f[g[x] = M[make_pair(g[l[x]], g[r[x]])]]++;
}
void Dfs(int x)
{
if (l[x]) Dfs(l[x]);
if (r[x]) Dfs(r[x]);
if (!M[make_pair(g[l[x]], g[r[x]])]) M[make_pair(g[l[x]], g[r[x]])] = ++cnt;
ans += f[g[x] = M[make_pair(g[l[x]], g[r[x]])]];
}
int main()
{
for (inone(T); T--;)
{
intwo(n, m); g[0] = cnt = 0;
rep(i, 1, n)
{
intwo(l[i], r[i]);
l[i] = max(l[i], 0);
r[i] = max(r[i], 0);
}
M.clear(); ms(f, 0); dfs(1);
rep(i, 1, m)
{
intwo(l[i], r[i]);
l[i] = max(l[i], 0);
r[i] = max(r[i], 0);
}
ans = 0; Dfs(1);
printf("%lld\n", ans);
}
return 0;
}
相关文章推荐
- zoj3549 快速幂
- ZOJ 1002
- ZOJ-3861 DFS+回溯
- ZOJ3279 —— 8G island
- zoj_1004-Anagrams by stack
- zoj_2278-Fight for Food
- ZOJ 1001 A+B Problem
- zoj 2710 Two Pipelines
- zoj 386 4000 1 Valid Pattern Lock
- ZOJ 3755 Mines 回溯+剪枝
- ZOJ3750 Dot Dot Dot 枚举状态+BFS
- poj 1514&zoj 1185 Metal Cutting(半平面交)
- ZOJ1101-Gamblers 终于AC。。。
- zoj 1148 The Game 一个晚上终于AC!
- zoj 3420 纯bfs
- PAT 01-复杂度2. Maximum Subsequence Sum (25)&&PAT 01-复杂度1. 最大子列和问题(20)
- 狗狗40题~ (Volume A)
- 狗狗40题~ (Volume B)
- 狗狗40题~ (Volume C)
- ZOJ3319 DP 通过入度出度判非法情况