zoom 8.10笔试第一题为什么20%,有大佬帮忙看看么
zoom 8.10笔试第一题为什么20%,有大佬帮忙看看么
import java.util.*; public class Main { static int ans = 0; static int red_color = 0, blue_color = 0; public static void dfs(List<Integer>[] edges, boolean[] vis, char[] color, int x) { if (vis[x]) return; vis[x] = true; if (color[x] == 'R') red_color++; if (color[x] == 'B') blue_color++; ans += Math.abs(blue_color - red_color); for (int nei : edges[x]) { if (!vis[nei]) dfs(edges, vis, color, nei); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String colorString = sc.next(); List<Integer>[] edges = new List[n]; boolean[] vis = new boolean[n]; char[] color = colorString.toCharArray(); for (int i = 0; i < n - 1; i++) { int s = sc.nextInt() - 1; int t = sc.nextInt() - 1; if (edges[s] == null) edges[s] = new ArrayList<>(); if (edges[t] == null) edges[t] = new ArrayList<>(); edges[s].add(t); edges[t].add(s); } for (int i = 0; i < n; i++) { if (!vis[i]) dfs(edges, vis, color, i); } System.out.println(ans); } }