UVA.839 Tree UVA 699

UVA.839 Tree
UVA 699

稍微用到了一点回溯思想的二叉树。

贴代码:

#include<iostream>
using namespace std;

bool solve(int& w){
	int W1, D1, W2, D2;
	bool b1 = true, 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
";
		else cout << "NO
";
		if(T) cout << "
";
	}
	return 0;
}

递归dfs读入,然后参数w是代表着子 天平的重量(最开始w是未知,然后递归到最深层时,开始读入W1,W2后往外返回w)