本题考查
树的深度优先遍历
思路
利用迭代方式实现DFS,并通过函数参数传递路径与路径权值的变化情况。
因为题目要求结果需要排序,将结果存入ArrayList中,并编写一个比较器进行排序。
最开始的一般我直接用String的排序方法,结果有一个用例没通过,分析得知是因为String的排序方法有一些漏洞比如说:“123”、“99”进行比较时,后者要大于前者,因为String是按照字符进行比较的。所以要想进行比较需要将代表顺序的字符串转化为整数进行比较。所以需要自己编写一个比较器。
AC代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static String[] weight;
static ArrayList<Integer>[] tree;
static int sum;
static ArrayList<String> result = new ArrayList<>();
static void dfs() {
DFS(0,"",0);
}
static void DFS(int start ,String path,int total) {
if(total > sum);
else {
ArrayList<Integer> current = tree[start];
path += String.valueOf(weight[start]) + " ";
total += Integer.parseInt(weight[start]);
if(current == null) {
if(total == sum)
result.add(path.trim());
}
else {
for(int i = 0 ; i < current.size() ; i++) {
DFS(current.get(i),path,total);
}
}
}
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner scaner = new Scanner(System.in);
int totalNodeNum = scaner.nextInt();
int nonLeafNodeNum = scaner.nextInt();
sum = scaner.nextInt();
scaner.nextLine();
tree = new ArrayList[totalNodeNum];
weight = scaner.nextLine().split(" ");
for(int i = 0 ; i < nonLeafNodeNum ; i++) {
int index = scaner.nextInt();
int num = scaner.nextInt();
ArrayList<Integer> temp = tree[index] = new ArrayList<>();
for(int j = 0 ; j < num ; j++) {
temp.add(scaner.nextInt());
}
}
scaner.close();
dfs();
Comparator<String> cmp = new newcmp();
Collections.sort(result,cmp);
for(String s : result)
System.out.println(s);
}
}
class newcmp implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
String[] temp1 = o1.split(" ");
String[] temp2 = o2.split(" ");
int edge = Math.min(temp1.length, temp2.length);
for(int i = 1 ; i < edge ; i++) {
if(temp1[i].equals(temp2[i]));
else {
int int1 = Integer.parseInt(temp1[i]);
int int2 = Integer.parseInt(temp2[i]);
if(int1 > int2) {
return -1;
}
else
return 1;
}
}
return 0;
}
}