Source code for submission s1004

Go to diff to previous submission

Main.java

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Main {
  5. static int N, E;
  6. static Node[] nodes;
  7. static ArrayList<Node> checks;
  8. public static void main(String args[]) throws Exception {
  9. String line;
  10.  
  11. while((line = br.readLine()) != null) {
  12. int a[] = new int[2];
  13. parseIntArr(line.toCharArray(), a);
  14. N = a[0]; E = a[1];
  15. nodes = new Node[N+1];
  16. ArrayList<Node> d3 = new ArrayList<Node>();
  17. for(int i = 1; i <= N; i++) nodes[i] = new Node();
  18.  
  19. boolean found = false;
  20.  
  21. for(int i = 0; i < E; i++) {
  22. if(!found) parseIntArr(br.readLine().toCharArray(), a);
  23. else {
  24. br.readLine();
  25. continue;
  26. }
  27.  
  28. nodes[a[0]].edges.add(a[1]);
  29. nodes[a[1]].edges.add(a[0]);
  30.  
  31. if(nodes[a[0]].edges.size() == 4) found = true;
  32. else if(nodes[a[1]].edges.size() == 4) found = true;
  33. else if(nodes[a[0]].edges.size() == 3) d3.add(nodes[a[0]]);
  34. else if(nodes[a[1]].edges.size() == 3) d3.add(nodes[a[1]]);
  35. }
  36.  
  37. if(found) System.out.println("YES");
  38. else {
  39. outer:
  40. for(Node n : d3) {
  41. checks = new ArrayList<Node>();
  42. walk(n);
  43. for(int i = 0; i < checks.size(); i++) {
  44. for(int j = i+1; j < checks.size(); j++) {
  45. if(check(checks.get(i), checks.get(j))) {
  46. found = true;
  47. break outer;
  48. }
  49. }
  50. }
  51. }
  52. if(found) System.out.println("YES");
  53. else System.out.println("NO");
  54. }
  55. }
  56. }
  57.  
  58. static void walk(Node n) {
  59. if(n.visited) return;
  60. else {
  61. n.visited = true;
  62. if(n.edges.size() >= 3) {
  63. checks.add(n);
  64. }
  65. for(Integer i : n.edges) walk(nodes[i]);
  66. }
  67. }
  68.  
  69. static boolean check(Node n1, Node n2) {
  70. HashSet<Integer> hs = new HashSet<Integer>(4);
  71. hs.addAll(n1.edges);
  72. for(Integer i : n2.edges) {
  73. if(hs.contains(i)) return false;
  74. }
  75. return true;
  76. }
  77.  
  78. public static void parseIntArr(char[] arr, int[] target)
  79. {
  80. int i = 0;
  81. int acc = -1;
  82.  
  83. for(char ch :arr)
  84. {
  85. if(ch == ' ')
  86. {
  87. if(acc!= -1)
  88. {
  89. target[i++] = acc;
  90. acc = -1;
  91. }
  92. }
  93. else if(acc == -1)
  94. acc = ch - '0';
  95. else
  96. acc = acc * 10 + (ch - '0');
  97. }
  98. if(acc != -1)
  99. target[i] = acc;
  100. }
  101. }
  102.  
  103. class Node {
  104. boolean visited = false;
  105. ArrayList<Integer> edges = new ArrayList<Integer>(4);
  106. }

Diff to submission s839

Main.java

--- c5.s839.cteam028.fn.java.0.Main.java
+++ c5.s1004.cteam028.fn.java.0.Main.java
@@ -5,4 +5,5 @@
   static int N, E;
   static Node[] nodes;
+  static ArrayList<Node> checks;
   public static void main(String args[]) throws Exception {
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
@@ -37,8 +38,15 @@
       if(found) System.out.println("YES");
       else {
+        outer:
         for(Node n : d3) {
-          if(walk(n, false)) {
-            found = true;
-            break;
+          checks = new ArrayList<Node>();
+          walk(n);
+          for(int i = 0; i < checks.size(); i++) {
+            for(int j = i+1; j < checks.size(); j++) {
+              if(check(checks.get(i), checks.get(j))) {
+                found = true;
+                break outer;
+              }
+            }
           }
         }
@@ -49,19 +57,22 @@
   }
 
-  static boolean walk(Node n, boolean foundFirst) {
-    if(n.visited) return false;
+  static void walk(Node n) {
+    if(n.visited) return;
     else {
       n.visited = true;
-      if(n.edges.size() == 3) {
-        if(foundFirst) return true;
-        else foundFirst = true;
+      if(n.edges.size() >= 3) {
+        checks.add(n);
       }
+      for(Integer i : n.edges) walk(nodes[i]);
+    }
+  }
 
-      for(Integer i : n.edges) {
-        boolean result = walk(nodes[i], foundFirst);
-        if(result) return true;
-      }
+  static boolean check(Node n1, Node n2) {
+    HashSet<Integer> hs = new HashSet<Integer>(4);
+    hs.addAll(n1.edges);
+    for(Integer i : n2.edges) {
+      if(hs.contains(i)) return false;
     }
-    return false;
+    return true;
   }