UVA839 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.

\epsfbox{p839a.eps}

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.

\epsfbox{p839b.eps}

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

题目大意:给你一个树形的天平,如图所示,根据力矩相等原则判断是否相等。

注意:输入的时候采用(先序)输入,如果WL为0时,该天平有左子天平,接下来

描述这个左子天平,如果WR为0时,该天平有有右子天平,接下来描述这个右

子天平。如果WL = WR = 0。则先描述左子天平,再描述右子天平。

思路:递归进行输入并进行判断。从最里层开始判断返回结果。一直到根。

#include<cstdio>
#include<iostream>
using namespace std;

bool solve(int &W)
{
    int W1,D1,W2,D2;
    bool b1 = true;
    bool b2 = true;
    cin >> W1 >> D1 >> W2 >> D2;
    if(!W1)//递归求解左子树
        b1 = solve(W1);
    if(!W2)//递归求解右子树
        b2 = solve(W2);
    W = W1 + W2;
    return b1 && b2 && (W1*D1 == W2*D2);//从最里层往上得到结果
}
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        int W;

        if(solve(W))
            cout << "YES" << endl;
        else
            cout << "NO" << endl;

        if(T)
            cout << endl;
    }

    return 0;
}
时间: 2024-10-12 09:33:27

UVA839 Not so Mobile【二叉树】【递归】的相关文章

UVA839 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 mobil

UVa839 Not so Mobile (二叉树的DFS)

链接:http://acm.hust.edu.cn/vjudge/problem/19486分析:二叉树模型的DFS.输入是采用递归方式输入,直接变读边判断就行. 1 #include <cstdio> 2 3 bool solve(int& W) { 4 int W1, D1, W2, D2; 5 scanf("%d%d%d%d", &W1, &D1, &W2, &D2); 6 bool b1 = true, b2 = true;

二叉树递归与非递归遍历,最近公共父节点算法

#include <iostream> #include <stack> using namespace std; #define MAX 100 //字符串最大长度 typedef struct Node //二叉树结点 { char data; Node *lchild,*rchild; } *Btree; void createBT(Btree &t); //先序构造二叉树 void preorder(Btree &t); //二叉树递归先序遍历 void i

二叉树 + 递归 + 分治法总结

二叉树递归相关题目的时间复杂度基本上都是O(n) = 一共有n个点 + 每个点的时间复杂度(1) 而二叉树分治法最坏的时间复杂度为O(n^2) 时间复杂度:T(n) = 2T(n/2) + O(1) = O(n) Merge Sort, Quick Sort: T(n) = 2T(n/2) + O(n) = O(nlogn) 前序遍历: 解法一:递归,通常是设置一个全局变量来保存结果. /** * Definition for a binary tree node. * struct TreeN

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

java 二叉树递归遍历算法

//递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRecursion(root); System.out.println(); } //层次遍历 public void layerorder() { System.out.print("binaryTree层次遍历:"); LinkedList<Node<Integer>>

【数据结构与算法】二叉树递归与非递归遍历(附完整源码)(转)

转自:http://blog.csdn.net/ns_code/article/details/12977901 二叉树是一种非常重要的数据结构,很多其他数据机构都是基于二叉树的基础演变过来的.二叉树有前.中.后三种遍历方式,因为树的本身就是用递归定义的,因此采用递归的方法实现三种遍历,不仅代码简洁且容易理解,但其开销也比较大,而若采用非递归方法实现三种遍历,则要用栈来模拟实现(递归也是用栈实现的).下面先简要介绍三种遍历方式的递归实现,再详细介绍三种遍历方式的非递归实现. 一.三种遍历方式的递

(二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

Return any binary tree that matches the given preorder and postorder traversals. Values in the traversals pre and post are distinct positive integers. Example 1: Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7] Note: 1 <=

[LeetCode]105. 从前序与中序遍历序列构造二叉树(递归)

题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 题解 递归 递归传参:该子树对应的前序遍历和中序遍历(用开始结束指针表示即可) 确定每层:new当前子树根节点,左右孩子分别赋值为递归的返回值 递归结束条件:返回当前子树根节点 使用HashMap记录当前子树根节点在中序遍历中的位置,方便每次查找. 利用左子树节点数量查找idx 代码 /** * Definition for a binary tree node. * public class TreeNo