Uva839天平Not so Mobile(二叉树结构)

题目

Before being an ubiquous(ubiquitous普遍存在的) communications gadget(小装置), a mobile was just a structure made of strings and wires suspending(悬挂) colourfull things. This kind of mobile is usually found hanging over cradles(摇篮) of small babies.

The figure illustrates(说明) a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum(支点) on the point where the string ties the wire. From the lever principle(原理) we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is Wl × Dl = Wr × Dr where Dl is the left distance,Dr is the right distance, Wl is the left weight and Wr is the right weight.

In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure.

In this case it is not so straightforward(简单) to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium(平衡) or not.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input is composed(组成) of several lines, each containing 4 integers separated by a single space.The 4 integers represent the distances of each object to the fulcrum and their weights, in the format:

Wl Dl Wr Dr

If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the the sub-mobile. In this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding(忽略) the weight of the wires and strings. If both Wl and Wr are zero then the following lines define two sub-mobiles: first the left then the right one.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

Write ‘YES’ if the mobile is in equilibrium, write ‘NO’ otherwise.

思路:

判断每层平衡并求和

存储,用结构体

代码:

/*
1

0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2

*/
//#define MAX 10000
#include<stdio.h>
#include<stdlib.h>
//#define LOCAL
typedef struct Mobile{
	int wl,wr;
	int dl,dr;
	int wSum;
//	int set;
	struct Mobile *left,*right;
}mbl;
mbl *root;
mbl* newnode(){
	mbl* u=(mbl*)malloc(sizeof(mbl));
	if(u!=NULL){
//		u->wl=u->wr=-1;
		u->wSum=0;
		u->fa=u->left=u->right=NULL;
	}
	return u;
}
//释放树 
void remove(mbl *u){
	if(u!=NULL){
		remove(u->left);
		remove(u->right);
		free(u);
	}
}
int mobile(mbl *u){
	if((u->dl*u->wl)==(u->dr*u->wr))return 1;
	else return 0;
}
//接收天平值,新建天平,并返回重量和或不平衡(用sum=-1代替 
int input(mbl *u) {
	int twl,twr,tdl,tdr;
	scanf("%d%d%d%d",&twl,&tdl,&twr,&tdr);
	u->dl=tdl;
	u->wl=twl;
	u->dr=tdr;
	u->wr=twr;
//	printf("wl:%d dl:%d wr:%d dr:%d\n",u->wl,u->dl,u->wr,u->dr) ;
//	if(twl&&twr){
//		if(!mobile(u)) u->wSum=-1;
//		else u->wSum=twl+twr;
//		//sum传给父结点还是直接return回结果 
//	}else
	if(!twl||!twr){
		mbl *v=newnode();
		if(!twl){
//			printf("!twl,dl:%d\t",tdl);
			u->left=v;
			u->wl=input(v);
//			printf("getwl:%d\n",u->wl);
		}
		if(!twr){
//			printf("!twr,dr:%d\t",tdr);
			u->right=v;
			u->wr=input(v);
//			printf("getwr:%d\n",u->wr);
		}
		if(twl==-1||twr==-1){
			u->wSum=-1;
		}
//		else{
//			if(!mobile(u)) u->wSum=-1;
//			else u->wSum=u->wl+u->wr;	
//		}
	}
	if(u->wSum!=-1){
		if(!mobile(u)) u->wSum=-1;
		else u->wSum=u->wl+u->wr;
	}
	return u->wSum;
}

int main(){	
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
	int n;//案例数 
	int flag;
	scanf("%d",&n);
	while(n--){
//		int twl,twr,tdl,tdr;
		root=newnode();
//		mbl *u=root;
		flag=input(root);
//		flag=mobile();
		
		if(flag!=-1)printf("YES\n");
		else printf("NO\n");
		if(n>0)printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lagoon_lala/article/details/81136646