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