Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class Solution {
  4. public static void main(String[] args) throws Exception {
  5. FastScanner sc = new FastScanner();
  6. PrintWriter pw = new PrintWriter(System.out);
  7. int t = sc.nextInt();
  8. for(int i=1;i<=t;i++) {
  9. int n = sc.nextInt();
  10. int m = sc.nextInt();
  11. gra g = new gra(n);
  12. for(int j=0;j<m;j++) {
  13. g.addEdge(sc.nextInt()-1, sc.nextInt()-1);
  14. }
  15. g.connectedComponents();
  16. pw.println("Case #"+i+": "+((n-(g.e-g.c)-1)*2+g.e-g.c));
  17. }
  18. pw.close();
  19. }
  20. }
  21. class gra {
  22. // A user define class to represent a graph.
  23. // A graph is an array of adjacency lists.
  24. // Size of array will be V (number of vertices
  25. // in graph)
  26. int V;
  27. int c;
  28. int e;
  29. LinkedList<Integer>[] adjListArray;
  30. boolean[] visited;
  31. // constructor
  32. gra(int V) {
  33. this.V = V;
  34. c=0;
  35. e=0;
  36. visited = new boolean[V];
  37. // define the size of array as
  38. // number of vertices
  39. adjListArray = new LinkedList[V];
  40. Arrays.fill(visited, true);
  41. // Create a new list for each vertex
  42. // such that adjacent nodes can be stored
  43.  
  44. for(int i = 0; i < V ; i++){
  45. adjListArray[i] = new LinkedList<Integer>();
  46. }
  47. }
  48.  
  49. // Adds an edge to an undirected graph
  50. void addEdge( int src, int dest) {
  51. // Add an edge from src to dest.
  52. adjListArray[src].add(dest);
  53. visited[src]=false;
  54. visited[dest]=false;
  55. // Since graph is undirected, add an edge from dest
  56. // to src also
  57. adjListArray[dest].add(src);
  58. }
  59.  
  60. void DFSUtil(int v, boolean[] visited) {
  61. // Mark the current node as visited and print it
  62. visited[v] = true;
  63. e++;
  64. // Recur for all the vertices
  65. // adjacent to this vertex
  66. for (int x : adjListArray[v]) {
  67. if(!visited[x]) DFSUtil(x,visited);
  68. }
  69.  
  70. }
  71. void connectedComponents() {
  72. // Mark all the vertices as not visited
  73. for(int v = 0; v < V; ++v) {
  74. if(!visited[v]) {
  75. // print all reachable vertices
  76. // from v
  77.  
  78. DFSUtil(v,visited);
  79. c++;
  80. }
  81. }
  82. }
  83. }
  84. @SuppressWarnings("all")
  85. class FastScanner {
  86. BufferedReader br;
  87. StringTokenizer st;
  88. public FastScanner(BufferedReader d) {
  89. br=d;
  90. }
  91. public FastScanner(String s) {
  92. try {
  93. br = new BufferedReader(new FileReader(s));
  94. } catch (FileNotFoundException e) {
  95. // TODO Auto-generated catch block
  96. e.printStackTrace();
  97. }
  98. }
  99.  
  100. public FastScanner() {
  101. br = new BufferedReader(new InputStreamReader(System.in));
  102. }
  103.  
  104. String nextToken() {
  105. while (st == null || !st.hasMoreElements()) {
  106. try {
  107. st = new StringTokenizer(br.readLine());
  108. } catch (IOException e) {
  109. // TODO Auto-generated catch block
  110. e.printStackTrace();
  111. }
  112. }
  113. return st.nextToken();
  114. }
  115.  
  116. int nextInt() {
  117. return Integer.parseInt(nextToken());
  118. }
  119.  
  120. long nextLong() {
  121. return Long.parseLong(nextToken());
  122. }
  123.  
  124. double nextDouble() {
  125. return Double.parseDouble(nextToken());
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement