fork(1) download
  1. import java.awt.Dimension;
  2. import java.io.*;
  3. import static java.lang.Character.*;
  4. import static java.lang.Math.*;
  5. import java.math.BigDecimal.*;
  6. import java.math.BigInteger.*;
  7. import static java.math.BigInteger.*;
  8. import java.util.*;
  9. import static java.util.Arrays.*;
  10.  
  11.  
  12. //<editor-fold defaultstate="collapsed" desc="Main">
  13. class Ideone {
  14. // https://n...content-available-to-author-only...s.org/kb/73/java/editor-codereference_ru.html#display
  15.  
  16. private void run() {
  17. try {
  18. Locale.setDefault(Locale.US);
  19. } catch (Exception e) {
  20. }
  21. boolean oj = true;
  22. try {
  23. oj = System.getProperty("MYLOCAL") == null;
  24. } catch (Exception e) {
  25. }
  26.  
  27. if (oj) {
  28. sc = new FastScanner(new InputStreamReader(System.in));
  29. out = new PrintWriter(new OutputStreamWriter(System.out));
  30. } else {
  31. try {
  32. sc = new FastScanner(new FileReader("input.txt"));
  33. out = new PrintWriter(new FileWriter("output.txt"));
  34. } catch (IOException e) {
  35. MLE();
  36. }
  37. }
  38. Solver s = new Solver();
  39. s.sc = sc;
  40. s.out = out;
  41. s.solve();
  42. if (!oj) {
  43. err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
  44. err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
  45. }
  46. out.flush();
  47. }
  48.  
  49. private void show(int[] arr) {
  50. for (int v : arr) {
  51. err.print(" " + v);
  52. }
  53. err.println();
  54. }
  55.  
  56. public static void exit() {
  57. err.println("Time: " + (System.currentTimeMillis() - timeBegin) / 1e3);
  58. err.printf("Mem: %d\n", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20);
  59. out.flush();
  60. out.close();
  61. System.exit(0);
  62. }
  63.  
  64. public static void MLE() {
  65. int[][] arr = new int[1024 * 1024][];
  66. for (int i = 0; i < arr.length; i++) {
  67. arr[i] = new int[1024 * 1024];
  68. }
  69. }
  70.  
  71. public static void main(String[] args) {
  72. new Ideone().run();
  73. }
  74.  
  75. static long timeBegin = System.currentTimeMillis();
  76. static FastScanner sc;
  77. static PrintWriter out;
  78. static PrintStream err = System.err;
  79. }
  80. //</editor-fold>
  81.  
  82. //<editor-fold defaultstate="collapsed" desc="FastScanner">
  83. class FastScanner {
  84.  
  85.  
  86. FastScanner(InputStreamReader reader) {
  87. br = new BufferedReader(reader);
  88. st = new StringTokenizer("");
  89. }
  90.  
  91. String next() {
  92. while (!st.hasMoreElements()) {
  93. try {
  94. st = new StringTokenizer(br.readLine());
  95. } catch (Exception ex) {
  96. return null;
  97. }
  98. }
  99. return st.nextToken();
  100. }
  101.  
  102. int nextInt() {
  103. return Integer.parseInt(next());
  104. }
  105.  
  106. long nextLong() {
  107. return Long.parseLong(next());
  108. }
  109.  
  110. double nextDouble() {
  111. return Double.parseDouble(next());
  112. }
  113.  
  114. String nextLine() {
  115. try {
  116. return br.readLine();
  117. } catch (IOException ex) {
  118. return null;
  119. }
  120. }
  121. }
  122. //</editor-fold>
  123.  
  124. class Solver {
  125.  
  126. void myAssert(boolean OK) {
  127. if (!OK) {
  128. Ideone.MLE();
  129. }
  130. }
  131.  
  132. FastScanner sc;
  133. static PrintStream err = System.err;
  134.  
  135. class Rib{
  136. int to, w;
  137.  
  138. Rib(int to, int w) {
  139. this.to = to;
  140. this.w = w;
  141. }
  142. }
  143.  
  144. int n;
  145. String[] place;
  146. int[][] person;
  147. ArrayList<Rib>[] gr;
  148. int[] color;
  149. TreeSet<Integer>[] comp;
  150.  
  151. void dfs( int v ){
  152. comp[color[v]].add(v);
  153. for( Rib r : gr[v] ){
  154. if( color[r.to] == -1 ){
  155. color[r.to] = color[v] ^ r.w;
  156. dfs( r.to );
  157. }
  158. myAssert( color[r.to] == (color[v] ^ r.w) );
  159. }
  160. }
  161.  
  162. void solve() {
  163. n = sc.nextInt();
  164. person = new int[n+1][];
  165. place = new String[n+1];
  166. for( int v = 1; v <= n; ++v ){
  167. place[v] = sc.next();
  168. person[v] = new int[sc.nextInt()];
  169. for (int j = 0; j < person[v].length; j++)
  170. person[v][j] = sc.nextInt();
  171. }
  172.  
  173. gr = new ArrayList[n+1];
  174. for (int v = 1; v <= n; v++)
  175. gr[v] = new ArrayList<>();
  176.  
  177.  
  178. for( int v = 1; v <= n; ++v ){
  179. for( int to : person[v] ){
  180. if( place[v].equals(place[to]) ){
  181. gr[v ].add(new Rib(to, 0));
  182. gr[to].add(new Rib( v, 0));
  183. }
  184. else{
  185. gr[v ].add(new Rib(to, 1));
  186. gr[to].add(new Rib( v, 1));
  187. }
  188. }
  189. }
  190.  
  191. TreeSet<Integer> ans = getAns();
  192.  
  193. out.println( ans.size() );
  194. for (Integer v : ans) {
  195. out.print( " " + v );
  196. myAssert( 1 <= v && v <= n );
  197. }
  198. out.println();
  199. }
  200.  
  201. TreeSet<Integer> getAns() {
  202. TreeSet<Integer> ans = new TreeSet<>();
  203. color = new int[n+1];
  204. fill( color, -1 );
  205. //int cntUsedVertex = 0;
  206. for (int v = 1; v <= n; v++) {
  207. if( color[v] == -1 ){
  208. color[v] = 0;
  209. comp = new TreeSet[2];
  210. comp[0] = new TreeSet<>();
  211. comp[1] = new TreeSet<>();
  212. dfs( v );
  213. //cntUsedVertex += comp[0].size() + comp[1].size();
  214. if( comp[0].size() <= comp[1].size() )
  215. ans.addAll(comp[0]);
  216. else
  217. ans.addAll(comp[1]);
  218. }
  219. }
  220. //myAssert( cntUsedVertex == n );
  221. if( ans.isEmpty() ) ans.add(1);
  222. return ans;
  223. }
  224.  
  225. }
Success #stdin #stdout 0.07s 380224KB
stdin
3
A  1   3
WC 1   3
WC 0
stdout
1
 1