[UVA839]Not so Mobile
算法入门经典第6章6-9(P157)
题目大意:输入一个树状天平,根据力矩相等原则判断是否平衡。
试题分析:貌似没有什么难点……
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; inline int read(){ int x=0,f=1;char c=getchar(); for(;!isdigit(c);c=getchar()) if(c=='-') f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f; } const int MAXN=100001; const int INF=999999; int N,M; int T; int solve(int &k){ int Wl,Wr,Dl,Dr; Wl=read(),Dl=read(),Wr=read(),Dr=read(); bool Ql=1,Qr=1; if(!Wl) Ql=solve(Wl); if(!Wr) Qr=solve(Wr); k=Wl+Wr; return Ql&&Qr&&(Wl*Dl==Wr*Dr); } int main(){ T=read(); while(T--){ if(solve(M)) puts("YES"); else puts("NO"); if(T) printf(" "); } }