UVA - 839

  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 bothWl 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

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 int ok;
 6
 7 int dfs() {
 8     int wl,dl,wr,dr;
 9     cin >> wl >> dl >> wr >> dr;
10     if (wl == 0) wl = dfs();
11     if (wr == 0) wr = dfs();
12     if (wl * dl != wr * dr) ok = 0;
13     else return wl + wr;
14 }
15
16 int main () {
17     int T;
18     freopen("1.in","r",stdin);
19     cin >> T;
20     while (T--) {
21         ok = 1;
22         dfs();
23         cout << (ok == 0 ? "NO" : "YES") << endl;
24         if (T) cout << endl;
25     }
26 }

时间: 2024-10-25 16:58:55

UVA - 839的相关文章

[2016-02-09][UVA][839][Not so Mobile]

时间:2016-02-09 12:10:40 星期二 题目编号:UVA 839 题目大意:给定一个秤,已知秤两边的重量和到支点的距离,问秤能否平衡 分析:每个秤都是一个树的结构,只需要遍历一遍,看每个分支点能否平衡即可 方法:根绝输入,dfs下去,判断每个节点是否平衡即可 解题过程遇到问题: 刚开始,不平衡,返回值设置成0,没考虑为0的情况,不平衡的时候 改成返回-1就AC了. 刚开始忘记输出每组数据之间的空行 这里写法把整棵树都遍历了一遍,事实上,当出现不平衡的点,读取完数据之后,就可以直接返

Uva 839 Not so Mobile

题意:这是一个类似于树的天平,这个天平的每一端都有可能由一个子天平构成,而每个天平都满足一个公式 WL * DL = WR * DR,其中WL,WR分别代表左边和右边物品的重量,DL,DR分别代表左边和右边物品里天平中心的距离. 输入分析:对于每个输入的四个数,如果WL或WR为0时,则代表接下来的输入代表子天平的数据,如果WL和WR同时为0,则输入数据先描述左子天平的状态,其次是右子天平. 要求:判断该数据是否可以构成一个平衡的天平. 思路:根据这个天平的描述,我很自然的想到了递归,有点类似于二

UVa 839 天平

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=780 先建立二叉树,之后遍历. 1 #include<iostream> 2 using namespace std; 3 4 struct Node 5 { 6 int W1, D1, W2, D2; 7 Node *left; 8 Node *right; 9 };

UVa 839 (递归方式读取二叉树) Not so Mobile

题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然能理解,但自己是写不出来的. 这里的参数是传引用,所以是在递归回来的时候才会赋值的. 1 //#define LOCAL 2 #include <iostream> 3 using namespace std; 4 5 bool solve(int& w) 6 { 7 int w1, d1

uva 839 not so mobile——yhx

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. (picture copy failed,cou h

UVA 839 Not so Mobile (递归建立二叉树)

题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归.直接递归求解即可. #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climit

uva 839 Not so Mobile-S.B.S.

Before being an ubiquous communications gadget, a mobilewas just a structure made of strings and wires suspendingcolourfull things. This kind of mobile is usually found hangingover cradles of small babies.The gure illustrates a simple mobile. It is j

UVA 839 Not so Mobile (递归建树 判断)

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<algorithm> #include<cctype> #include<sstream&

UVa 839 Not so Mobile(树的递归输入)

题意  判断一个树状天平是否平衡   每个测试样例每行4个数  wl,dl,wr,dr  当wl*dl=wr*dr时  视为这个天平平衡  当wl或wr等于0是  下一行将是一个子天平  如果子天平平衡  wl为子天平的wl+wr  否则整个天平不平衡 容易看出  输入是递归的  所以我们可以直接递归  边输入边判断 #include<cstdio> using namespace std; bool solve(int &w) { int wl, dl, wr, dr; bool m