UVA839-Not so Mobile

Posted npugen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 ?gure 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 ?gure. 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 de?ne 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 de?ne two sub-mobiles: ?rst 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 }

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





以上是关于UVA839-Not so Mobile的主要内容,如果未能解决你的问题,请参考以下文章

UVA839 Not so Mobile递归树

UVa 839 Not so Mobile

uva 839 Not so Mobile

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

Uva 839 Not so Mobile

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