Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution
- {
- void dfs(int i, boolean[] vis, ArrayList<ArrayList<Integer>> adj){
- if(vis[i]) return;
- vis[i] = true;
- for(ArrayList<Integer> arr : adj){
- for(Integer j : arr){
- dfs(j.intValue(), vis, adj);
- }
- }
- }
- //Function to find a Mother Vertex in the Graph.
- public int findMotherVertex(int V, ArrayList<ArrayList<Integer>>adj)
- {
- ArrayList<Integer> z = new ArrayList<>();
- for(int k = 0; k < V; k++){
- boolean[] vis = new boolean[V];
- for(int i = 0; i < V; i++){
- vis[i] = false;
- }
- dfs(k,vis,adj);
- boolean flg = true;
- for(int i = 0; i < V; i++){
- if(!vis[i]){
- flg = false;
- break;
- }
- }
- if(flg) z.add(k);
- }
- for(ArrayList<Integer> arr : adj){
- for(Integer j : arr){
- System.out.print(j + " ");
- }
- System.out.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement