2025.5.8 某为机试题 文件目录
作者:
小彭要奋斗
,
2025-05-08 23:50:08
· 广东
,
所有人可见
,
阅读 3
import java.util.*;
class Main(){
public static List<String> split(String str, String c){
return Arrays.asList(str.split(c));
}
public static int dfs(String id, Map<String, List<String>> childMap, Map<String, Integer> directorySize){
int res = directorySize.getOrDefault(id, 0);
List<String> childs = childMap.getOrDefault(id, new ArrayList<>());
for(String s : childs){
res += dfs(s, childMap, directorySize);
}
return res;
}
public static void main(String[] argv){
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
in.nextLine();
Map<String, List<String>> childMap = new HashMap<>();
Map<String, Integer> directorySize = new HashMap<>();
for(int i = 0;i < m;i++){
String[] param = in.nextLine().split(" ");
String id = param[0];
int size = Integer.parseInt(param[1]);
directorySize.put(id, size);
if(param[2].length(0) == 2) continue;
String childstr = param[2].substring(1, param[2].length() - 1);
List<String> childList = childstr.isEmpty() ? new ArrayList<>() : param[2].split(",");
childMap.put(id, childList);
}
Sytem.out.println(dfs(String.value(n), childMap, directorySize));
}
}