Question 1520: Tree sub-structure-9 degrees

Source: Internet
Author: User
Description:

Enter Two Binary Trees A and B to determine whether B is a sub-structure.

Input:

The input may contain multiple test examples. The input ends with EOF.
For each test case, the input first line is an integer n, m (1 <= n <= 1000, 1 <= m <= ): N indicates the number of nodes of the Binary Tree A to be input (the number of nodes starts from 1), and M indicates the number of nodes of the Binary Tree B to be input (the number of nodes starts from 1 ). The next row contains N numbers, each representing the I-th element of the tree, and the next n rows. The first Ki number represents the number of children on the I-th node, next there are ki trees, representing the node I child node number. The next m + 1 line is the same as the description of tree.

Output:

Corresponding to each test case,
If B is a, the subtree outputs "yes" (excluding quotation marks ). Otherwise, "no" is output (excluding quotation marks ).

Sample input:
7 38 8 7 9 2 4 72 2 32 4 5002 6 7008 9 22 2 3001 12030
Sample output:
YESNO
Tip:

B is not a subtree of any tree.

Recommendation index :※

Source: http://ac.jobdu.com/problem.php? PID = 1, 1520

The algorithm itself is difficult. Pay attention to the following points:

1. Be careful when using the vector and time out ..

2. The default subtree condition of the question is: None, left and right. Don't think it's complicated.

#include<iostream>#include<string.h>#include <stdio.h>#include<stdlib.h>using namespace std;const int N=1001;int a_edge[N][2],b_edge[N][2];int a_val[N],b_val[N];int n,m;bool dfs_sub(int a,int b){//sub tree is fitif(b==0)        return true;if(a_val[a]!=b_val[b]||false==dfs_sub(a_edge[a][0],b_edge[b][0])||false==dfs_sub(a_edge[a][1],b_edge[b][1]))return false;return true;}bool is_sub(){    int i;for(i=1;i<=n;i++){if(a_val[i]==b_val[1]){if(true==dfs_sub(i,1))return true;}}return false;}int main(){    while(scanf("%d%d",&n,&m)!=EOF){memset(a_val,0,sizeof(a_val));memset(b_val,0,sizeof(b_val));        int i,j,num;for(i=1;i<=n;i++)scanf("%d",&a_val[i]);for(i=1;i<=n;i++){scanf("%d",&num);for(j=0;j<num;j++)scanf("%d",&a_edge[i][j]);}for(i=1;i<=m;i++)scanf("%d",&b_val[i]);for(i=1;i<=m;i++){int num,tmp;scanf("%d",&num);for(j=0;j<num;j++)scanf("%d",&b_edge[i][j]);}if(m!=0&&n!=0&&true==is_sub())printf("YES\n");elseprintf("NO\n");    }    return 0;}

In the version with the last case timeout of a vector attached:

#include<iostream>#include<vector>#include<string>#include <stdio.h>#include<stdlib.h>using namespace std;vector<vector<int> > a_edge;vector<vector<int> > b_edge;int *a_val,*b_val;int n,m;bool dfs_sub(int a_node,int b_node){//sub tree is fit    if(a_val[a_node]==b_val[b_node]){if(b_edge[b_node].size()<a_edge[a_node].size()){int i,j;if(b_edge[b_node].size()==0)                return true;else{                if(dfs_sub(a_edge[a_node][0],b_edge[b_node][0])==true)return true;            }        }else if(b_edge[b_node].size()==a_edge[a_node].size()){int i;bool flag=false;for(i=0;i<b_edge[b_node].size();i++){if(false==dfs_sub(a_edge[a_node][i],b_edge[b_node][i]))return false;}return true;}    }    return false;}bool is_sub(){    int i;for(i=1;i<=n;i++){if(a_val[i]==b_val[1]){if(true==dfs_sub(i,1))return true;}}return false;}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        a_val=new int [n+1];        b_val=new int [m+1];        a_edge.resize(n+1);        b_edge.resize(m+1);        int i,j;for(i=1;i<=n;i++)scanf("%d",&a_val[i]);for(i=1;i<=n;i++){int num,tmp;scanf("%d",&num);for(j=1;j<=num;j++){scanf("%d",&tmp);a_edge[i].push_back(tmp);}}for(i=1;i<=m;i++)scanf("%d",&b_val[i]);for(i=1;i<=m;i++){int num,tmp;scanf("%d",&num);for(j=1;j<=num;j++){scanf("%d",&tmp);b_edge[i].push_back(tmp);}}if(m!=0&&n!=0&&true==is_sub())printf("YES\n");elseprintf("NO\n");    }    return 0;}

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.