第二题,每个结点 U 到子树中某结点 V 的最大边权和,有大佬帮我看看吗? import java.util.*; public class Main {     static int [] dparray_weight; //每个结点 U 到子树中某结点 V 的最大边权和     static int [][] tree_edge; //记录边的权值     static int n;  //结点个数     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         n = Integer.valueOf(sc.nextLine());         tree_edge = new int[n][n];         dparray_weight = new int[n];         init_tree_edge(); //初始化边矩阵,自己到自己为0,其余为int的最大值         //保存输入的边         for(int i=1;i<n;i++){             String[] temp = sc.nextLine().split(" ");             int u = Integer.valueOf(temp[0])-1;             int v=  Integer.valueOf(temp[1])-1;             tree_edge[u][v] = Integer.valueOf(temp[2]);         }         dp_weight(0);         for(int i=0;i<n;i++)             System.out.print(dparray_weight[i]+" ");     }     public static int dp_weight(int index){         if( isleaf(index) ){             dparray_weight[index]=0;             return  dparray_weight[index];         }         for(int j=0;j<n;j++)             if( j!=index && tree_edge[index][j] != Integer.MAX_VALUE ) //index到j有一条边                 dparray_weight[index]=Math.max(dparray_weight[index], dp_weight(j)+ tree_edge[index][j]);         return  dparray_weight[index];     }     public static boolean isleaf(int index){ //结点下标为index的结点是不是叶子结点         boolean flag =true;         for(int j=0;j<n;j++)             if( tree_edge[index][j] != Integer.MAX_VALUE && index!=j){                 flag = false;                 break;             }         return flag;     }     public static void init_tree_edge(){         for(int i=0;i<n;i++)             for (int j=0;j<n;j++)                 if(i==j)                     tree_edge[i][j]=0;                 else                     tree_edge[i][j]=  Integer.MAX_VALUE;     }     public static void print_array(){         for(int i=0;i<n;i++){             for (int j=0;j<n;j++)                 System.out.print(tree_edge[i][j]+" ");             System.out.println();         }     } }
点赞 2

相关推荐

01-04 11:28
已编辑
广州华立学院 Java
程序员花海:实习内容写的看起来太偏向于技术了 要结合业务 很多同学搞反了 其实业务才是最重要的 要避免这种单纯罗列技术栈的格式
简历被挂麻了,求建议
点赞 评论 收藏
分享
喵_coding:这实习干啥了? 没写出来一点产出啊 好好回忆一下干了啥 问问ai 问问大佬 准备些话术就好了
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务