Ultraviolet A 839 Not so Mobile (recursive input of the tree), uva839

Source: Internet
Author: User

Ultraviolet A 839 Not so Mobile (recursive input of the tree), uva839

Determine if a tree balance is balanced. In each test example, there are 4 wl, dl, wr in each row, dr when wl * dl = wr * dr, this balance is considered as this balance. When wl or wr is equal to 0, the next line will be a sub-balance. If the sub-balance wl is the sub-balance's wl + wr, otherwise the entire balance is unbalanced.

It is easy to see that the input is recursive, so we can directly recursive the input edge to judge.

#include<cstdio>using namespace std;bool solve(int &w){    int wl, dl, wr, dr;    bool mobl = true, mobr = true;    scanf("%d %d %d %d", &wl, &dl, &wr, &dr);    if(!wl) mobl = solve(wl);    if(!wr) mobr = solve(wr);    w = wl + wr;    return mobl && mobr && (wl * dl == wr * dr);}int main(){    int cas, w;    scanf("%d", &cas);    while(cas--)    {        printf(solve(w) ? "YES\n" : "NO\n");        if(cas) printf("\n");    }    return 0;}



Not so Mobile

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 isWDL =WDRwhereDL 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 bothWL andWRare 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

Not so Mobile

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 isWDL =WDRwhereDL 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 bothWL andWRare 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



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.