Zjuoj 3602 Count the Trees

Source: Internet
Author: User

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 have at most of the 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 it descendants in T. Binary trees is called identical if their left subtrees is the same (or both have no left subtree) and their right Subtrees is the same (or both has no right subtrees).

According to a recent, some people in the world is interested in counting the number of identical subtree pairs, Each from the given trees respectively.

Now, you are given and trees. Write a program to help to count the number of identical subtree pairs, such that first one comes from the first tree And the second one comes from the second tree.

Input

There is 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 all test case, there is integers and n m (1≤ n, m ≤100000) indicating the number of nodes in the given The trees. The following n lines describe the first tree. iThe-th line contains the integers u v and (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 has the corresponding left or right child. Then followed by m lines describing the second tree in the same format. The roots of both trees is node 1.

Output

For each test case, print a line containing the result.

Sample Input
22 2-1 2-1 -12-1-1-15 52 34 5-1-1-1-1-1-12 34 5-1-1-1-1-1-1
Sample Output
111
Hint

The "Trees" in the first sample.

References
    • Http://en.wikipedia.org/wiki/Binary_tree

Author: Zhuang, Junyuan; WU, Zejun
Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

AC Code:

1#include <stdio.h>2#include <string.h>3#include <algorithm>4 5 using namespacestd;6 7 #defineMAXN 1000108 9 structEdge {Ten     intV, E, label; OneEdge *link; A} EDGE[MAXN], *ADJ[MAXN]; -  - intTotE; the intF[MAXN], G[MAXN], VAL[MAXN], DEGREE[MAXN], FA[MAXN]; - intvec[2], Q[MAXN]; - intA = -, B =7, pp = One, q =1000000007; -  + voidAddedge (intUintVintlabel) { -Edge *p = &edge[tote++]; +P->v =v; AP->label =label; atP->link =Adj[u]; -Adj[u] =p; - } -  - voidCalintNintF[MAXN]) { -TotE =0; inmemset (adj, NULL,sizeof(adj)); -memset (Degree,0,sizeof(degree)); to      for(inti =1; I <= N; ++i) { +         intL, R; -scanf"%d%d", &l, &R); the         if(L! =-1) { *Addedge (i, L,137); $++Degree[i];Panax NotoginsengFA[L] =i; -         } the         if(r! =-1) { +Addedge (i, R,1007); A++Degree[i]; theFA[R] =i; +         } -     } $     intL =0, r =0; $      for(inti =1; I <= N; ++i) { -         if(!Degree[i]) -q[r++] =i; the     } -      while(L! =r) {Wuyi         intU = q[l++]; theEdge *p =Adj[u]; -         intTotal =0; Wu          while(p) { -vec[total++] = (Long Long) val[p->v] * P->label%Q; Aboutp = p->link; $         } -Val[u] =A; -          for(inti =0; I < total; ++i) { -Val[u] = (Long Long) val[u] * pp% q ^ vec[i]%Q; A         } +         if(--degree[fa[u]] = =0) q[r++] =Fa[u]; the     } -      for(inti =1; I <= N; ++i) $F[i] =Val[i]; theSort (f +1, F +1+n); the } the  the intMain () { -     intT; inscanf"%d", &T); the      for(intCAS =1; CAS <= T; ++CAs) { the         intN, M; Aboutscanf"%d%d", &n, &m); the Cal (n, f); the Cal (M, g); the         intP1 =1, p2 =1; +         Long Longres =0; -          while(P1 <= n && p2 <=m) { the             if(F[P1] >G[P2])Bayi++P2; the             Else if(F[P1] <G[P2]) the++P1; -             Else { -                 intP3 =P1; the                  while(P3 +1<= N && f[p3 +1] ==F[P1]) the++P3; the                 intP4 =P2; the                  while(P4 +1<= m && G[p4 +1] ==G[P2]) -++P4; theRes + = (Long Long) (P3-p1 +1) * (P4-P2 +1); theP1 = p3 +1; theP2 = P4 +1;94             } the         } theprintf"%lld\n", res); the     }98     return 0; About}
View Code

Zjuoj 3602 Count the Trees

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.