1 条题解

  • 0
    @ 2025-7-1 22:26:21

    java题解:

    import java.io.*;
    import java.util.*;
    
    public class Main {
        static int n, m;
        static List<List<Integer>> adj;
        static String[] names;
        static int[] weight;
        static boolean[] visited;
        static long bestSum = 0;
        static String bestNode = "";
        
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            n = Integer.parseInt(br.readLine().trim());  // 读取节点数
            
            names = new String[n];
            weight = new int[n];
            Map<String, Integer> idMap = new HashMap<>();
            
            // 读取每个节点的名称和权重
            for (int i = 0; i < n; i++) {
                String[] parts = br.readLine().split(" ");
                names[i] = parts[0];
                weight[i] = Integer.parseInt(parts[1]);
                idMap.put(names[i], i);
            }
            
            m = Integer.parseInt(br.readLine().trim());  // 读取连接关系数量
            
            // 初始化邻接表
            adj = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                adj.add(new ArrayList<>());
            }
            
            // 构建无向图
            for (int i = 0; i < m; i++) {
                String[] edge = br.readLine().split(" ");
                int u = idMap.get(edge[0]);
                int v = idMap.get(edge[1]);
                adj.get(u).add(v);
                adj.get(v).add(u);
            }
            
            // 查找所有连通分量
            visited = new boolean[n];
            for (int i = 0; i < n; i++) {
                if (!visited[i]) {
                    // 使用改进的DFS方法
                    Pair result = improvedDfs(i);
                    if (result.sum > bestSum) {
                        bestSum = result.sum;
                        bestNode = names[result.maxWeightNodeIndex];
                    }
                }
            }
            
            System.out.println(bestNode + " " + bestSum);
        }
        
        // 改进的DFS方法,返回连通分量的总权重和权重最大的节点索引
        static Pair improvedDfs(int u) {
            visited[u] = true;
            long sum = weight[u];
            int maxWeightNodeIndex = u;
            
            for (int v : adj.get(u)) {
                if (!visited[v]) {
                    Pair childResult = improvedDfs(v);
                    sum += childResult.sum;
                    
                    // 更新当前连通分量中权重最大的节点
                    if (weight[childResult.maxWeightNodeIndex] > weight[maxWeightNodeIndex]) {
                        maxWeightNodeIndex = childResult.maxWeightNodeIndex;
                    }
                }
            }
            
            return new Pair(sum, maxWeightNodeIndex);
        }
        
        // 用于存储DFS结果的辅助类
        static class Pair {
            long sum;  // 连通分量的总权重
            int maxWeightNodeIndex;  // 连通分量中权重最大的节点索引
            
            Pair(long sum, int maxWeightNodeIndex) {
                this.sum = sum;
                this.maxWeightNodeIndex = maxWeightNodeIndex;
            }
        }
    }
    
    
    
    • 1

    25年6月-华为实习-2.连通网络节点和

    信息

    ID
    13
    时间
    1000ms
    内存
    256MiB
    难度
    3
    标签
    递交数
    4
    已通过
    1
    上传者