|
|
back to boardWA#7 please help me in JAVA 1.7 import java.io.*; import java.util.*; class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } } public class _1106 { public static void main(String args[]) throws IOException { int a; Reader.init(System.in); int n = Reader.nextInt(); int graph[][] = new int[n][n]; ArrayList<ArrayList<Integer>> st = new ArrayList<ArrayList<Integer>>(); ArrayList<Integer> grp1 = new ArrayList<Integer>(); ArrayList<Integer> grp2 = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { ArrayList<Integer> ar = new ArrayList<Integer>(); do { a = Reader.nextInt(); if (a != 0) { graph[i][a - 1] = 1; ar.add(a - 1); } } while (a != 0); st.add(ar); } if (n != 0) { grp1.add(0); for (int i = 1; i < n; i++) { if (graph[0][i] != 1) { grp1.add(i); } else { grp2.add(i); } } for (int i = 1; i < grp1.size(); i++) { int x = grp1.get(i); if (!st.get(x).isEmpty()) { boolean frFlag = false; Iterator it = st.get(x).iterator(); while (it.hasNext()) { if (grp2.contains((Integer) it.next())) { frFlag = true; continue; } } if (!frFlag) { Integer rm = st.get(x).get(0); //grp1.r grp1.remove(rm); grp2.add(rm); } } } System.out.println(grp1.size()); Iterator it1 = grp1.iterator(); while (it1.hasNext()) { System.out.print(((Integer) it1.next() + 1) + " "); } } else { System.out.println(0); } } } |
|
|