Before being an ubiquous communications gadget,MobileWas 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 has strates a simple mobile. it is just a wire, suincluded 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 isWL×DL =WR×DR whereDL is
The left distance,DR is the right distance,WL is the left weight andWR 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:WLDLWRDR
IfWL orWR is zero then there is a sub-mobile hanging from that end and the following lines define 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 bothWLandWR 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.
Sample Input
10 2 0 40 3 0 11 1 1 12 4 4 21 6 3 2
Sample output
YES
Determines whether a tree is a mechanical equilibrium (leverage theorem). If it is balanced, yes is output; otherwise no is output.
Solution: Build and update W and Judge at the same time.
#include<iostream>using namespace std;#include<cstdio>struct node{ int wl,dl,wr,dr; node*left,*right; node(int a,int b,int c,int d) :wl(a),dl(b),wr(c),dr(d),left(0),right(0){}};node*build(){ int a,b,c,d; cin>>a>>b>>c>>d; node*root=new node(a,b,c,d); if(root->wl==0) { root->left=build(); root->wl+=root->left->wl+root->left->wr; } if(root->wr==0) { root->right=build(); root->wr+=root->right->wl+root->right->wr; } return root;}bool judge(node*root){ if(root) { if(root->wl*root->dl!=root->wr*root->dr)return false; else if(!judge(root->left))return false; else if(!judge(root->right))return false; } return true;}void see(node*root){ if(root) { cout<<root->wl<<endl; cout<<root->dl<<endl; cout<<root->wr<<endl; cout<<root->dr<<endl; cout<<"***************"<<endl; see(root->left); see(root->right); }}int main(){ int cas; cin>>cas; for(int i=0;i<cas;i++) { node* root=build(); //see(root); if(judge(root)) cout<<"YES"<<endl; else cout<<"NO"<<endl; if(i!=cas-1)cout<<endl; } return 0;}