fork download
  1.  
  2. import java.io.BufferedInputStream;
  3. import java.io.IOException;
  4.  
  5. public class Main {
  6. static class FastScanner {
  7. private final BufferedInputStream in = new BufferedInputStream(System.in);
  8.  
  9. int nextInt() throws IOException {
  10. int c;
  11. do {
  12. c = in.read();
  13. } while (c <= ' ' && c != -1);
  14.  
  15. int num = 0;
  16. while (c > ' ') {
  17. num = num * 10 + c - '0';
  18. c = in.read();
  19. }
  20. return num;
  21. }
  22. }
  23.  
  24. public static void main(String[] args) throws IOException {
  25. FastScanner fs = new FastScanner();
  26.  
  27. int n = fs.nextInt();
  28. int m = fs.nextInt();
  29.  
  30. int[] head = new int[n + 1];
  31. int[] to = new int[2 * m];
  32. int[] next = new int[2 * m];
  33. int[] degree = new int[n + 1];
  34.  
  35. java.util.Arrays.fill(head, -1);
  36.  
  37. int edgeIndex = 0;
  38.  
  39. for (int i = 0; i < m; i++) {
  40. int u = fs.nextInt();
  41. int v = fs.nextInt();
  42.  
  43. to[edgeIndex] = v;
  44. next[edgeIndex] = head[u];
  45. head[u] = edgeIndex++;
  46.  
  47. to[edgeIndex] = u;
  48. next[edgeIndex] = head[v];
  49. head[v] = edgeIndex++;
  50.  
  51. degree[u]++;
  52. degree[v]++;
  53. }
  54.  
  55. boolean[] visited = new boolean[n + 1];
  56. int[] stack = new int[n];
  57. int answer = 0;
  58.  
  59. for (int i = 1; i <= n; i++) {
  60. if (visited[i]) {
  61. continue;
  62. }
  63.  
  64. int top = 0;
  65. stack[top++] = i;
  66. visited[i] = true;
  67.  
  68. boolean isCycle = true;
  69.  
  70. while (top > 0) {
  71. int u = stack[--top];
  72.  
  73. if (degree[u] != 2) {
  74. isCycle = false;
  75. }
  76.  
  77. for (int e = head[u]; e != -1; e = next[e]) {
  78. int v = to[e];
  79.  
  80. if (!visited[v]) {
  81. visited[v] = true;
  82. stack[top++] = v;
  83. }
  84. }
  85. }
  86.  
  87. if (isCycle) {
  88. answer++;
  89. }
  90. }
  91.  
  92. System.out.println(answer);
  93. }
  94. }
  95.  
Success #stdin #stdout 0.07s 40376KB
stdin
Standard input is empty
stdout
0