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. 若 ...
随机推荐
- 程序员的冷笑话 python版本
在伯乐在线上看到了个冷笑话,感觉很有意思. void tellStory() { printf("从前有座山\n"); printf("山上有座庙\n"); p ...
- 【springmvc+mybatis项目实战】杰信商贸-2.数据库配置
首先我们来了解项目的架构 我们分别使用了MySql和Oracle数据库,即是异构数据库.我们做到一个平台支持多个数据库.数据库建模我们使用Sybase公司的PowerDesigner(以后简称PD), ...
- 《Effective C++》读书笔记 资源管理
C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...
- 56[LeetCode] .Merge Intervals
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
- Beta版本软件使用说明
北京航空航天大学计算机学院 远航1617 小组 产品版本: Beta版本 产品名称:Crawling is going on 文档作者:杨帆 文档日期:2013/12/24 1. 引言 1.1 ...
- 软件工程 speedsnail 第二次冲刺9
20150526 完成任务:划线的优化,速度和谐: 遇到问题: 问题1 速度仍然不满意 解决1 未解决 明日任务: 蜗牛碰到线后速度方向的调整:(做优化)
- LintCode-69.二叉树的层次遍历
二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 返回他的分层遍历结果: [ [3], [9,2 ...
- Swift-闭包理解(二)
简明扼要的闭包表达式 其实Swift已经为我们提供了很多简化的语法,可以让我们保证代码的高可读性和维护性.还用上面的例子来说明,对于 greetPeople 这个全局函数来说,其实只需要使用一次,所 ...
- laravel5.6 后台无法退出,必须清楚浏览器缓存才能退出
方法一: 在后台,admin/logincontroleer.php 中 可行 public function logout(Request $request) { Auth::logout(); ...