UVA.839 Not so Mobile ( 二叉树 DFS)

UVA.839 Not so Mobile ( 二叉树 DFS)

题意分析

给出一份天平,判断天平是否平衡。
一开始使用的是保存每个节点,节点存储着两边的质量和距离,但是一直是Runtime error。也不知道到底是哪里出了问题,后来发现直接判断当前是否平衡,若下面还有节点,接着递归调用dfs判断,这样一来省去了存储节点所需要的空间和时间,效率大大提升。

代码总览

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#define nmax 10000000
#define INF 0x3f3f3f3f
using namespace std;
struct node{
    int lwei,rwei;
    int ldis,rdis;
    bool risend,lisend;
};
bool isbalence;
bool cal (struct node p)
{
    if(p.ldis * p.lwei == p.rdis * p.rwei) return true;
    else return false;
}
int dfs()
{
    struct node p;
    scanf("%d%d%d%d",&p.lwei,&p.ldis,&p.rwei,&p.rdis);
    if(p.lwei == 0) p.lwei = dfs();
    if(p.rwei == 0) p.rwei = dfs();
    if(!cal(p)) isbalence = false;
    return (p.lwei+p.rwei);
}
int main()
{
    int t;
    scanf("%d",&t);
    bool first = true;
    while(t--){
        isbalence = true;
        dfs();
        if(t) printf("%s\n\n",isbalence?"YES":"NO");
        else printf("%s\n",isbalence?"YES":"NO");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值