Advertisement
enigmjoe

tmp

Sep 5th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. class Solution
  2. {
  3. void dfs(int i, boolean[] vis, ArrayList<ArrayList<Integer>> adj){
  4. if(vis[i]) return;
  5. vis[i] = true;
  6. for(ArrayList<Integer> arr : adj){
  7. for(Integer j : arr){
  8. dfs(j.intValue(), vis, adj);
  9. }
  10. }
  11. }
  12. //Function to find a Mother Vertex in the Graph.
  13. public int findMotherVertex(int V, ArrayList<ArrayList<Integer>>adj)
  14. {
  15. ArrayList<Integer> z = new ArrayList<>();
  16. for(int k = 0; k < V; k++){
  17. boolean[] vis = new boolean[V];
  18. for(int i = 0; i < V; i++){
  19. vis[i] = false;
  20. }
  21. dfs(k,vis,adj);
  22. boolean flg = true;
  23. for(int i = 0; i < V; i++){
  24. if(!vis[i]){
  25. flg = false;
  26. break;
  27. }
  28. }
  29. if(flg) z.add(k);
  30. }
  31. for(ArrayList<Integer> arr : adj){
  32. for(Integer j : arr){
  33. System.out.print(j + " ");
  34. }
  35. System.out.println();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement