uva839

import com.sun.org.apache.xpath.internal.operations.Bool;

import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Scanner;


public class Main {
    static Scanner cin;
    static PrintWriter pw;

    public static class Node{
        public Node() {

        }

        public Node(int wl, int wr, int dl, int dr) {
            this.wl = wl;
            this.wr = wr;
            this.dl = dl;
            this.dr = dr;
        }

        Node l = null;
        Node r = null;
        int wl,wr,dl,dr;
        int w; //自身重量
    }

    public static Node buildTree() {
        int wl, wr, dl, dr;
        wl = cin.nextInt();
        dl = cin.nextInt();
        wr = cin.nextInt();
        dr = cin.nextInt();

        Node node = new Node(wl, wr, dl, dr);
        if (node.wl == 0) {
            node.l = buildTree();
        }

        if (node.wr == 0) {
            node.r = buildTree();
        }

        return node; //返回根節點

    }

    public static  boolean  DFS(Node root) {

        Boolean l = true;
        Boolean r = true;

        if (root.l != null) {
            l = DFS(root.l);
            root.wl = root.l.w;
        }

        if (root.r != null) {
            r = DFS(root.r);
            root.wr = root.r.w;
        }

        root.w = root.wl + root.wr;

        if (l && r && root.dl * root.wl == root.dr * root.wr) {
            return true;
        }else{
            return false;
        }

    }


    public static void main(String[] args) {
        try {


            cin = new Scanner(System.in);
            pw = new PrintWriter(System.out);

            int kase;

            Node root = null;
            kase = cin.nextInt();
            while (kase-- !=0) {
                int wl, wr, dl, dr;
                root = buildTree();
                boolean balance = DFS(root);

                if (balance) {
                    System.out.println("YES");
                }else{
                    System.out.println("NO");
                }
                if (kase != 0) {
                    System.out.println();
                }

            }

            pw.flush();
            cin.close();
            pw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }




    }





}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值