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\n");
66         else printf("NO\n");
67         if(iCase) printf("\n");
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\n");
20         else printf("NO\n");
21         if(T) printf("\n");
22     }
23     return 0;
24 }

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

转载于:https://www.cnblogs.com/npugen/p/9502585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值