[树+递归]uva-839-Not so Mobile

发布于 2019-08-05  955 次阅读


https://vjudge.net/problem/UVA-839

题意 就是说给一个天平 这个天平可能有子天平 要么左右子树都有 要么都没有 不存在只有一个的情况 那样就肯定不平衡了 给出w d(重量和中心距) 求是否平衡 按照先序的顺序给出 那么就可以用递归了

#include <set>
#include<iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
bool solve(int& w) {
	int w1, d1, w2, d2;
	bool b1 = true;
	bool b2 = true;
	cin >> w1 >> d1 >> w2 >> d2;
	if (!w1) b1 = solve(w1);//左子树子天平
	if (!w2) b2 = solve(w2);//右子树子天平
	w = w1 + w2;//重量之和等于左右相加
	return b1 && b2 && (w1 * d1 == w2 * d2);//左天平 右天平都相等时 以及自身力矩相等时 才相等
}
int main() {
	int t, w;
	cin >> t;
	while (t--) {
		if (solve(w)) cout << "YES\n";
		else cout << "NO\n";
		if (t) cout << "\n";
	}
	return 0;
}

愿风指引你的道路,愿你的刀刃永远锋利。