思路:由于是通过递归的方式输入,于是编写一个递归过程进行输入比较好。(思路,题目图片取自紫书)
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<sstream>
using namespace std;
bool work(int & W){
int W1,D1,W2,D2;
bool son_1 = true,son_2 = true; //假定现在这个点的两个子天平都平衡(如无子天平,则其两个点是平衡的)
cin>>W1>>D1>>W2>>D2;
if(!W1) son_1 = work(W1); //判断子天平W1是否平衡,并求出W1的重量
if(!W2) son_2 = work(W2);
W = W1 + W2; //用来返回为作为其父天平的砝码重量
return son_1&&son_2&&(W1*D1==W2*D2); //两个子天平平衡且父天平力矩相等
}
int main()
{
int T,W;
cin>>T;
while(T--){
if(work(W)) printf("YES\n");
else printf("NO\n");
if(T) cout<<endl;
}
return 0;
}