河南省第九届省赛~~Prototypes analyze

Prototypes analyze

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
描述

ALpha Ceiling Manufacturers (ACM) is analyzing the properties of its new series of Incredibly Collapse-Proof Ceilings (ICPCs).  An ICPC consists of n layers of material, each with a different value of collapse resistance (measured as a positive integer). The analysis ACM wants to run will take the collapse-resistance values of the layers, store them in a binary search tree, and check whether the shape of this tree in any way correlates with the quality of the whole construction. Because, well, why should it not?  To be precise, ACM takes the collapse-resistance values for the layers, ordered from the top layer to the bottom layer,  and inserts them one-by-one into a tree. The rules for inserting a value v are:

• If the tree is empty, make v the root of the tree.

 If the tree is not empty, compare v with the root of the tree.

 If v is smaller, insert v into the left subtree of the root,

 otherwise insert v into the right subtree.

 

ACM has a set of ceiling prototypes it wants to analyze by trying to collapse them. It wants to take each group of ceiling prototypes that have trees of the same shape and analyze them together. For example , assume ACM is considering five ceiling prototypes with three layers each, as described by Sample Input 1 and shown in Figure C.1. Notice that the first prototype’s top layer has collapseresistance value 2, the middle layer has value 7, and the bottom layer has value 1. The second prototype has layers with collapse-resistance values of 3, 1, and 4 – and yet these two prototypes induce the same tree shape, so ACM will analyze them together. Given a set of prototypes, your task is to determine how many different tree shapes they induce.

输入
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8).
Each test case specifies :
● Line 1: two integers n (1 ≤ n ≤ 50), which is the number of ceiling prototypes to analyze,
and k (1 ≤ k ≤ 20), which is the number of layers in each of the prototypes.
● The next n lines describe the ceiling prototypes. Each of these lines contains k distinct
integers ( between 1 and 1e6, inclusive ) , which are the collapse-resistance values of the
layers in a ceiling prototype, ordered from top to bottom.
输出
For each test case generate a single line containing a single integer that is the number of different tree
shapes.
样例输入
15 32 7 11 5 93 1 42 6 59 7 3
样例输出
4

来源    河南省第九届省赛

解题思路:    先建树,在查询建立的数的形状是不是相同的

代码如下:

# include<stdio.h>
# include<string.h>
struct node{
	int key;
	node *right, * lift;
	node(int e):key(e),lift(NULL),right(NULL){}
};
int n,m;
int a[100][100];
node * Buid_Tree(int k,node *tree)         //建树过程 
{
	if(!tree)
	{
	return new node(k);	
	}
	if(tree->key < k)
	tree->right=Buid_Tree(k,tree->right);
	else tree->lift=Buid_Tree(k,tree->lift);
	return tree;
}
int find(node *r1,node * r2){        //查找两棵树是不是相同的 
	if(!r1&&!r2)
	return 1;
	else if(r1&&r2)
	return find(r1->lift,r2->lift)&&find(r1->right,r2->right); //我们只判断相同位置是不是存在结点,不用判断结点是否相同 
	return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--)
	{   node * tree[55];  //************
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;i++)
		{	tree[i]=NULL;
			for(int j=0;j<m;j++)
			{
				scanf("%d",&a[i][j]);
				tree[i]=Buid_Tree(a[i][j],tree[i]);				
			}	
		}
		int v[55]={0};//标记 
		int count=0;
		for(int i=0;i<n;i++)  //观察有几种不同的树 
		{	if(!v[i])
			{
				for(int j=i+1;j<n;j++)
			{
				if(!v[j])
				{	
					if(find(tree[i],tree[j]))
						v[j]=1;	
				}
			}
			v[i]=1;
			count++;   //记录 
			}
		}
	printf("%d\n",count);			
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/wx2306/article/details/80083056