• UVA839-Not so Mobile


    Problem UVA839-Not so Mobile

    Accept: 2663  Submit: 16417

    Time Limit: 3000 mSec

    Problem Description

    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

    题解:题目并不难,就是二叉树先序遍历,从一开始不会写递归函数到现在能够轻松搞定此类简单的递归函数,虽然有一点改变,但是看到lrj的代码之后就发现自己还是太水了,我写了

    两个递归函数,但是,都是先序遍历,为什么要写两个呢,写成一个,省了1/3的时间。

    我的代码

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 using namespace std;
     6 
     7 const int maxn = 100000+10;
     8 const int root = 1;
     9 int lchild[maxn],rchild[maxn];
    10 int val[maxn],ldis[maxn],rdis[maxn];
    11 int cnt;
    12 
    13 void newtree(){
    14     lchild[root] = rchild[root] = 0;
    15     val[root] = -1;
    16     cnt = root;
    17 }
    18 
    19 int newnode(){
    20     int u = ++cnt;
    21     lchild[u] = rchild[u] = 0;
    22     val[u] = -1;
    23     return u;
    24 }
    25 
    26 void build(int u){
    27     int w1,d1,w2,d2;
    28     scanf("%d%d%d%d",&w1,&d1,&w2,&d2);
    29     ldis[u] = d1,rdis[u] = d2;
    30     lchild[u] = newnode();
    31     if(w1 == 0) build(lchild[u]);
    32     else{
    33         val[lchild[u]] = w1;
    34     }
    35     rchild[u] = newnode();
    36     if(w2 == 0) build(rchild[u]);
    37     else{
    38         val[rchild[u]] = w2;
    39         return;
    40     }
    41 }
    42 
    43 bool check(int u,int &w){
    44     if(!lchild[u] && !rchild[u]){
    45         w = val[u];
    46         return true;
    47     }
    48     int w1,w2;
    49     if(check(lchild[u],w1) && check(rchild[u],w2) && w1*ldis[u]==w2*rdis[u]){
    50         w = w1+w2;
    51         return true;
    52     }
    53     else return false;
    54 }
    55 
    56 int main()
    57 {
    58     //freopen("input.txt","r",stdin);
    59     int iCase;
    60     scanf("%d",&iCase);
    61     while(iCase--){
    62         newtree();
    63         build(root);
    64         int W = 0;
    65         if(check(root,W)) printf("YES
    ");
    66         else printf("NO
    ");
    67         if(iCase) printf("
    ");
    68     }
    69     return 0;
    70 }

    lrj的代码

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 bool solve(int &w){
     6     int wl,dl,wr,dr;
     7     bool b1=true,b2=true;
     8     scanf("%d%d%d%d",&wl,&dl,&wr,&dr);
     9     if(!wl) b1=solve(wl);
    10     if(!wr) b2=solve(wr);
    11     w=wl+wr;
    12     return b1 && b2 && (wl*dl==wr*dr);
    13 }
    14 int main()
    15 {
    16     int T,w;
    17     scanf("%d",&T);
    18     while(T--){
    19         if(solve(w)) printf("YES
    ");
    20         else printf("NO
    ");
    21         if(T) printf("
    ");
    22     }
    23     return 0;
    24 }

    不仅快,而且代码短,要学的还有很多。

  • 相关阅读:
    解决 网上下载的例子 My Mac 64bit 不能运行的问题
    给XMLHttpRequest设置超时时间
    MSN常见登录错误解决方法
    javascript keycode大全
    禁止手动修改FileUpload控件
    "ORA00942: 表或视图不存在 "的原因和解决方法
    梅花雪控件树应用实例----异步绑定自定义表结构的用户部门表
    菜鸟学模式三 观察者模式
    菜鸟学模式一 序言
    利用js去除打印时的页眉页脚
  • 原文地址:https://www.cnblogs.com/npugen/p/9502585.html
Copyright © 2020-2023  润新知