Not so Mobile
Not so Mobile |
Before being an ubiquous 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.
Sample Input
1 0 2 0 4 0 3 0 1 1 1 1 1 2 4 4 2 1 6 3 2
Sample Output
YES
Jose Paulo Leal, ACM-UP'2001
今天在做比赛的时候做到了这道题,其实关键就是建树,但是由于我建树的时候没有加引用,检查了很长时间 也没检查出来,刚才突然想到引用问题看了一下代码,加上引用就调试过了,以前在没学引用的时候还知道会给函数形参另开辟一段空间,所以写程序的时候也很清楚,知道要返回那个值,现在学了引用不用返回值了,在主函数内便可得到子函数运行的结果,变量与他的别名共同占用同一段空间。反而更容易出错了,因为经常忽略忘了加引用,以后要注意嗷
#include <iostream>
#include <string.h>
#include <malloc.h>
using namespace std;
class tree
{
public:
int w,l,tag;
tree *lchild,*rchild;
};
tree *head;
int k=0;
int main()
{
void build(tree *&p1,tree *&p2);
void deal(tree *p);
int i,j,n,m,s,t;
tree *p;
cin>>n;
while(n--)
{
k=0;
head=new(tree);
head->w=0; head->l=1;
head->tag=1;
build(head->lchild,head->rchild);
deal(head);
if(!k)
{
cout<<"YES"<<endl;
}else
{
cout<<"NO"<<endl;
}
}
return 0;
}
void build(tree *&p1,tree *&p2)
{
int WL,DL,WR,DR;
tree *p3;
cin>>WL>>DL>>WR>>DR;
p1=new(tree);
p1->w=WL; p1->l=DL;
p2=new(tree);
p2->w=WR; p2->l=DR;
if(WL)
{
p1->tag=0;
p1->lchild=NULL;
p1->rchild=NULL;
}
if(WR)
{
p2->tag=0;
p2->lchild=NULL;
p2->rchild=NULL;
}
if(!WL)
{
p1->tag=1;
build(p1->lchild,p1->rchild);
}
if(!WR)
{
p2->tag=1;
build(p2->lchild,p2->rchild);
}
}
void deal(tree *p)
{
int w1,l1,w2,l2;
if(p)
{
deal(p->lchild);
deal(p->rchild);
if(p->tag==1)
{
w1=p->lchild->w; l1=p->lchild->l;
w2=p->rchild->w; l2=p->rchild->l;
if(w1*l1!=w2*l2)
{
k=1;
return ;
}
p->w+=(w1+w2);
}
}
}