小弟的😁 import java.util.HashMap; import java.util.Scanner; public class Main {     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int n = in.nextInt();         for (int i = 0; i < n; i++) {             int nodes = in.nextInt();             HashMap<Integer, Pair> map = new HashMap<>();             int[] depth = new int[nodes];             for (int j = 0; j < nodes; j++) {                 Pair pair = new Pair(in.nextInt(), in.nextInt(), in.nextInt());                 map.put(j, pair);                 update(depth, map, pair.left, j);                 update(depth, map, pair.right, j);             }             int maxDepth = Integer.MIN_VALUE;             for (int j = 0; j < nodes; j++) {                 maxDepth = Math.max(maxDepth, depth[j]);             }             int[] lines = new int[maxDepth + 1];             for (int j = 0; j < nodes; j++) {                 lines[depth[j]] += map.get(j).weight;             }             boolean flag = false;             for (int j = 1; j <= maxDepth; j++) {                 if (lines[j] <= lines[j - 1]) {                     flag = true;                     break;                 }             }             System.out.println(flag ? "NO" : "YES");         }     }     private static void update(int[] depth, HashMap<Integer, Pair> map, int cur, int root) {         if (cur == -1) {             return;         }         depth[cur] = depth[root] + 1;         if (map.containsKey(cur)) {             Pair pair = map.get(cur);             update(depth, map, pair.left, cur);             update(depth, map, pair.right, cur);         }     } } class Pair {     int weight = 0;     int left = -1;     int right = -1;     public Pair(int weight, int left, int right) {         this.weight = weight;         this.left = left;         this.right = right;     } }
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务