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);
    }
}


#ZOOM笔试##做完zoom2023秋招笔试,人麻了#
全部评论
你back track之后难道不撤销red_color 和blue_color的个数么,这个全局变量岂不是会一直累积
1
送花
回复
分享
发布于 2022-08-10 22:41
深搜没回溯啊
点赞
送花
回复
分享
发布于 2022-08-10 22:50
秋招专场
校招火热招聘中
官网直投
噢噢,发现问题了
点赞
送花
回复
分享
发布于 2022-08-10 22:52

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务