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(); } } }
uva839
最新推荐文章于 2023-02-15 17:12:27 发布