fork download
  1. import java.io.OutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.PrintWriter;
  5. import java.util.InputMismatchException;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.io.InputStream;
  9.  
  10. /**
  11.  * Built using CHelper plug-in
  12.  * Actual solution is at the top
  13.  *
  14.  * @author Jeel Vaishnav
  15.  */
  16. public class Solution {
  17. public static void main(String[] args) {
  18. InputStream inputStream = System.in;
  19. OutputStream outputStream = System.out;
  20. InputReader in = new InputReader(inputStream);
  21. PrintWriter out = new PrintWriter(outputStream);
  22. CherriesMesh solver = new CherriesMesh();
  23. int testCount = Integer.parseInt(in.next());
  24. for (int i = 1; i <= testCount; i++)
  25. solver.solve(i, in, out);
  26. out.close();
  27. }
  28.  
  29. static class CherriesMesh {
  30. ArrayList<Integer>[] adj;
  31. int[] vis;
  32.  
  33. void dfs(int i) {
  34. vis[i] = 1;
  35. for (int j : adj[i]) {
  36. if (vis[j] == 0)
  37. dfs(j);
  38. }
  39. }
  40.  
  41. public void solve(int testNumber, InputReader sc, PrintWriter out) {
  42. int n = sc.nextInt();
  43. int m = sc.nextInt();
  44.  
  45. adj = new ArrayList[n];
  46. for (int i = 0; i < n; ++i)
  47. adj[i] = new ArrayList<>();
  48.  
  49. for (int i = 0; i < m; ++i) {
  50. int u = sc.nextInt() - 1;
  51. int v = sc.nextInt() - 1;
  52. adj[u].add(v);
  53. adj[v].add(u);
  54. }
  55.  
  56. vis = new int[n];
  57. int comp = 0;
  58. for (int i = 0; i < n; ++i) {
  59. if (vis[i] == 0) {
  60. dfs(i);
  61. comp++;
  62. }
  63. }
  64.  
  65. int red = comp - 1;
  66. int black = n - 1 - red;
  67. int ans = black + red * 2;
  68.  
  69. out.println("Case #" + testNumber + ": " + ans);
  70. }
  71.  
  72. }
  73.  
  74. static class InputReader {
  75. private InputStream stream;
  76. private byte[] buf = new byte[1024];
  77. private int curChar;
  78. private int numChars;
  79. private InputReader.SpaceCharFilter filter;
  80.  
  81. public InputReader(InputStream stream) {
  82. this.stream = stream;
  83. }
  84.  
  85. public int read() {
  86. if (numChars == -1)
  87. throw new InputMismatchException();
  88.  
  89. if (curChar >= numChars) {
  90. curChar = 0;
  91. try {
  92. numChars = stream.read(buf);
  93. } catch (IOException e) {
  94. throw new InputMismatchException();
  95. }
  96.  
  97. if (numChars <= 0)
  98. return -1;
  99. }
  100. return buf[curChar++];
  101. }
  102.  
  103. public int nextInt() {
  104. int c = read();
  105.  
  106. while (isSpaceChar(c))
  107. c = read();
  108.  
  109. int sgn = 1;
  110.  
  111. if (c == '-') {
  112. sgn = -1;
  113. c = read();
  114. }
  115.  
  116. int res = 0;
  117. do {
  118. if (c < '0' || c > '9')
  119. throw new InputMismatchException();
  120. res *= 10;
  121. res += c - '0';
  122. c = read();
  123. }
  124. while (!isSpaceChar(c));
  125.  
  126. return res * sgn;
  127. }
  128.  
  129. public String readString() {
  130. int c = read();
  131. while (isSpaceChar(c))
  132. c = read();
  133. StringBuilder res = new StringBuilder();
  134. do {
  135. res.appendCodePoint(c);
  136. c = read();
  137. }
  138. while (!isSpaceChar(c));
  139.  
  140. return res.toString();
  141. }
  142.  
  143. public boolean isSpaceChar(int c) {
  144. if (filter != null)
  145. return filter.isSpaceChar(c);
  146. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  147. }
  148.  
  149. public String next() {
  150. return readString();
  151. }
  152.  
  153. public interface SpaceCharFilter {
  154. public boolean isSpaceChar(int ch);
  155.  
  156. }
  157.  
  158. }
  159. }
  160.  
  161.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:16: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty