建图通过环检测找到作弊的人 注意输入输出

火眼金睛

https://www.nowcoder.com/practice/d311403b15b8495b81b622edaefd5b5a?tpId=182&tqId=34666&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26pageSize%3D50%26search%3D%26tab%3D%25E5%2590%258D%25E4%25BC%2581%25E7%259C%259F%25E9%25A2%2598%26topicId%3D182&difficulty=undefined&judgeStatus=undefined&tags=&title=

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    //邻接表
    static List<List<Integer>> graph;
    static int[] visited;
    static boolean[] hasZuoBi;
    static int res;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int N = sc.nextInt();
            graph = new ArrayList<>(); // 初始化邻接表
            visited = new int[N];
            hasZuoBi = new boolean[N];
            res = 0; // 重置结果计数器
            for (int i = 0; i < N; i++) {
                graph.add(new ArrayList<>());
            }
            for (int i = 0; i < N; i++) {
                graph.add(new ArrayList<>());
                int node = sc.nextInt() - 1;
                int k = sc.nextInt();
                for (int j = 0; j < k; j++) {
                    int nodeNext = sc.nextInt() - 1;
                    graph.get(node).add(nodeNext);
                }
            }
            // System.out.println(graph);
            //第一轮去找双向箭头的节点即成环的
            for (int i = 0; i < N; i++) {
                dfs(i, graph);
            }
            // System.out.println(Arrays.toString(hasZuoBi));
            //找到连接两个作弊的节点
            for (int i = 0; i < N; i++) {
                if (hasZuoBi[i]) {
                    continue;
                } else {
                    List<Integer> temp = graph.get(i);
                    if (temp.size() >= 2) {
                        boolean tag = true;
                        for (int nodeNext : temp) {
                            //如果有一个没作弊
                            if (!hasZuoBi[nodeNext]) {
                                tag = false;
                                break;
                            }
                        }
                        hasZuoBi[i] = tag;
                    }
                }
            }
            for (int i = 0; i < N; i++) {
                if (hasZuoBi[i]) {
                    res++;
                }
            }
            System.out.println(res);
            if (res != 0) {
                for (int i = 0; i < N; i++) {
                    if (hasZuoBi[i]) {
                        System.out.print((i + 1) + " ");
                    }
                }
                //在多个case里面进行换行
                System.out.println(); // 换行
            }
        }
    }

    private static void dfs(int node, List<List<Integer>> graph) {
        if (visited[node] == -1) {
            return;
        }
        visited[node] = 1;
        for (int nodeNext : graph.get(node)) {
            if (node == nodeNext) continue;
            if (visited[nodeNext] == 1) {
                hasZuoBi[nodeNext] = true;
                hasZuoBi[node] = true;
                return;
            } else if (visited[nodeNext] == 0) {
                dfs(nodeNext, graph);
            }
        }
        visited[node] = -1;
    }
}

全部评论

相关推荐

美丽的查理斯不讲武德:包kpi的啊,感觉虾皮一点hc都没有
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:48
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务