uva 839 Not so Mobile

击打开链接

题目意思:给定一序列的数字,每组四个,分别是W1 D1 W2 D2 表示左右节点的重量和长度,如果W1 或W2 为0说明有子节点存在,判断是否满足平衡

解题思路:1 建立二叉树,然后遍历查找 但是这个很麻烦  2 dfs 我们知道对于一颗树肯定是要先把左子树建完才能建右子树 我们可以用递归方式来代替建树直接求出左右两边的重量,判断是否相等  3 硬搞,这题数据很水,直接判断叶子节点是否全部满足平衡,如果是YES,其它都是NO(话说这样和标称ans不同,但是也可以AC哦)

代码1(dfs):

//直接dfs,我们知道对于有0它肯定一直往下建树
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int t , w1 , w2 , d1 , d2 , flag;
//递归求解
void dfs(){
    scanf("%d%d%d%d" , &w1 , &d1 , &w2 , &d2);
    if(w1 == 0)
        w1 = dfs();
    if(w2 == 0)
        w2 = dfs();
    if(w1*d1 != w2*d2)
        flag = 0;
    return w1+w2;//返回重量
}
int main(){
    cin>>t;
    while(t--){
        flag = 1;//初始化为1
        dfs();
        if(flag)
            printf("YES\n");
        if(flag == 0)
            printf("NO\n");
        if(t)
            cout<<endl;
    }
}

代码2

/*这一题数据很水,所以我用直接判断叶子节点(对应的重量值全部为正数)对应是否全部满足平衡,如果有意个不满足就输出NO,否则YES.*/
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

const int MAXN = 1000010;
int  t , line , Index ,  node[MAXN][4] , flag;//node数组存储读入的数据

//处理问题的函数
void solve(){
    flag = 1;
    int i , j , lsum  = 0, rsum = 0;
    for(i = 0 ; i < Index ; i++){
       for(j = 0 ; j < 4 ; j++){
           if(node[i][0] == 0 || node[i][2] == 0)//这一句重点,如果是重量为0不用考虑
               break;
       }
       if(j == 4){
           lsum = node[i][0] * node[i][1];
           rsum = node[i][2] * node[i][3];
           if(lsum != rsum){//如果有意个不满足直接输出NO  return回去
               printf("NO\n");
               return;
           }
      }
    }
    if(i == Index)//如果全部满足输出YES
        printf("YES\n");
}
//主函数
int main(){
    int i , j;
    cin>>t;
    for(j = 1 ; j <= t ; j++){
        line = 0 ; Index = 1;//Index表示有几行,只要重量为0,Index加一
        while(1){
            for(i = 0 ; i < 4 ; i++)
               scanf("%d" , &node[line][i]);
            if(node[line][0] == 0)
                   Index++;
            if(node[line][2] == 0)
                   Index++;
            ++line;
            if(line == Index)
                break;
        }
        solve();
        if(j != t)
            cout<<endl;
    }
    return 0;
}
时间: 2024-12-21 03:41:42

uva 839 Not so Mobile的相关文章

UVa 839 Not so Mobile:DFS二叉树

839 - Not so Mobile Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=780 Before being an ubiquous communications gadget, a mobile was just a structure mad

UVa 402 M*A*S*H (STL&amp;amp;list)

402 - M*A*S*H Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=343 Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean W

UVa 10168 Summation of Four Primes:“1+1+1+1”问题

10168 - Summation of Four Primes Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1109 思路:既然1+1都在很大的数据范围内成立,那我们不妨先分出两个素数来,然后把剩下的数分成两个素数. 完整代码: 01./*0.025s*

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

jQuery Mobile的学习 jQuery Mobile工具栏、标题栏、页脚栏的定位学习

程序员都很赖,你懂的! 最近在做html5页面的开发,主要做智能终端设备的开发.对于内容比较少的页面,领导提出了要将页眉和页脚定位到网页的最上方和最下方.对于这样的要求,其实一点也不过分.但对于新手来说,确实很难,很不容易,今天我就将我学习的内容一起分享一下!放置页眉和页脚的方式有三种:     Inline - 默认.页眉和页脚与页面内容位于行内.     Fixed - 页面和页脚会留在页面顶部和底部.     Fullscreen - 与 fixed 类似;页面和页脚会留在页面顶部和底部请

jQuery Mobile教程:表单selectmenu插件

文章简介:本文我们来深度认识一下jQuery Mobile的selectmenu插件. 本文我们来深度认识一下jQuery Mobile的selectmenu插件 主要让大家熟悉一下动态创建selectmenu.禁用selectmenu以及启用selectmenu 1.动态创建input 示例: //动态创建 $("#dynamic_selectmenu").bind('click',function(){ //一次哦 if($("#your_choice_new"

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence