题意:判断一棵树是否符合力矩相等。
分析:递归。注意引用传值的使用。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1}; const int dc[] = {-1, 1, 0, 0}; const double pi = acos(-1.0); const double eps = 1e-8; const int MAXN = 10000 + 10; const int MAXT = 1000000 + 10; using namespace std; bool dfs(int &w){ int wl, dl, wr, dr; scanf("%d%d%d%d", &wl, &dl, &wr, &dr); bool flag1 = true; bool flag2 = true; if(!wl){ flag1 = dfs(wl); } if(!wr){ flag2 = dfs(wr); } w = wl + wr;//因为传参,所以此处计算了两者父结点的重量 return flag1 && flag2 && (wl * dl == wr * dr); } int main(){ int T; scanf("%d", &T); while(T--){ int w = 0; if(dfs(w)) printf("YES\n"); else printf("NO\n"); if(T) printf("\n"); } return 0; }