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;
}
UVA.839 Not so Mobile ( 二叉树 DFS)的更多相关文章
- UVa 839 -- Not so Mobile(树的递归输入)
UVa 839 Not so Mobile(树的递归输入) 判断一个树状天平是否平衡,每个测试样例每行4个数 wl,dl,wr,dr,当wl*dl=wr*dr时,视为这个天平平衡,当wl或wr等于0是 ...
- UVa 839 (递归方式读取二叉树) Not so Mobile
题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...
- UVA 839 Not so Mobile (递归建立二叉树)
题目连接:http://acm.hust.edu.cn/vjudge/problem/19486 给你一个杠杆两端的物体的质量和力臂,如果质量为零,则下面是一个杠杆,判断是否所有杠杆平衡. 分析:递归 ...
- uva 839 not so mobile——yhx
Not so Mobile Before being an ubiquous communications gadget, a mobile was just a structure made of ...
- UVa 839 Not so Mobile (递归思想处理树)
Before being an ubiquous communications gadget, a mobilewas just a structure made of strings and wir ...
- Uva 839 Not so Mobile
0.最后输出的yes no的大小写 1.注意 递归边界 一直到没有左右子树 即b1=b2=false的时候 才返回 是否 天平平衡. 2.注意重量是利用引用来传递的 #include <io ...
- UVA.548 Tree(二叉树 DFS)
UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- UVA.679 Dropping Balls (二叉树 思维题)
UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...
随机推荐
- Windows运行机理——创建窗口
Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 Windows 窗口在创建之前,其属性必须设定好,所谓属性包括类的名字 ...
- git服务器搭建及eclipse使用git
一.搭建git服务器 1.yum install git 2.新建用户linux用户git,管理git服务 useradd git passwd git 3.初始化git仓库 git init --b ...
- NGUI组件整理总结
一图流: 注意: private void RClickUI(Vector3 newPos) { this.gameObject.SetActive(true); this.transform.loc ...
- Spring 配置String转Date
操作步骤: 1. 实现 org.springframework.core.convert.converter.Converter 接口 2. 配置 org.springframework.contex ...
- [JSON].exists( keyPath )
语法:[JSON].exists( keyPath ) 返回:[True | False] 说明:检测指定键名路径是否存在 示例: Set jsonObj = toJson("{div:{' ...
- ajax 和 mock 数据
ajax ajax是一种技术方案,但并不是一种新技术.它依赖的是现有的CSS/HTML/Javascript,而其中最核心的依赖是浏览器提供的XMLHttpRequest对象,是这个对象使得浏览器可以 ...
- win10下搭建私链
首先要下载geth,下载地址:https://gethstore.blob.core.windows.net/builds/geth-windows-amd64-1.7.0-6c6c7b2a.exe ...
- css3美化radio样式
.magic-radio{ position: absolute; display: none; } .magic-radio + label { position: relative; displa ...
- defineporperty 的使用 设置对象的只读或只写属性
<!DOCTYPE html> <html lang="en"> <head> <title>Document</title& ...
- c#和.net区别
.net 包含两大部分:.net framework类库和公共语言运行库(CLR) .net framework类库,就是微软工程师写好的各种功能类,例如math类. 公共语言运行库:1.与操作系统进 ...