Created
February 13, 2021 05:42
-
-
Save kshitijvarshne1/934abee47904afe33e1bdf0b475653ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Created by IntelliJ IDEA. | |
* Author: Kshitij Varshney (kshitijvarshne1) | |
* Date: 13-Feb-21 | |
* Time: 10:41 AM | |
* File: GreyNodes.java | |
*/ | |
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class GreyNodes { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n= sc.nextInt(); | |
int [] array= new int[n]; | |
int [] graph= new int[n+2]; | |
int [] revGraph= new int[n+2]; | |
for (int i=0;i<= n+1;i++){ | |
for (int j=0;j<=n+1;j++){ | |
int x= sc.nextInt(); | |
if(x==1) { | |
graph[i] = j; | |
revGraph[j]=i; | |
} | |
} | |
} | |
for (int i=0;i<n;i++){ | |
int x= sc.nextInt(); | |
array[i]=x; | |
} | |
boolean [] visited= new boolean[n+2]; | |
boolean [] recStack= new boolean[n+2]; | |
for (int i=0;i<n+2;i++){ | |
visited[i]= false; | |
recStack[i]= false; | |
} | |
for (int i=0;i<n+2;i++){ | |
if(cycle(i,visited,recStack,graph)){ | |
System.out.println("True"); | |
System.exit(0); | |
} | |
} | |
for (int i=0;i<=n+1;i++){ | |
visited[i]=false; | |
} | |
dfs(0,graph,visited); | |
for (int i=0;i<= n+1;i++){ | |
if(visited[i]=false){ | |
System.out.println(i); | |
System.out.println("False"); | |
System.exit(0); | |
} | |
visited[i]= false; | |
} | |
dfs(n+1,revGraph,visited); | |
for (int i=0;i<=n+1;i++){ | |
if(visited[i]==false){ | |
System.out.println("False"); | |
System.exit(0); | |
} | |
visited[i]= false; | |
} | |
System.out.println("True"); | |
} | |
public static boolean cycle(int v, boolean[] visited, boolean[] recStack, int[] graph){ | |
if(visited[v]==false){ | |
visited[v]=true; | |
recStack[v]=true; | |
for(var i: graph){ | |
if(!visited[i] && cycle(i,visited,recStack,graph)){ | |
return true; | |
} | |
else if (recStack[i]){ | |
return true; | |
} | |
} | |
} | |
recStack[v]= false; | |
return false; | |
} | |
public static boolean isCycle(ArrayList<Integer> graph,int n){ | |
return false; | |
} | |
public static void dfs(int v,int[] graph, boolean [] visited){ | |
visited[v]= true; | |
for(var x : graph){ | |
if(!visited[x]){ | |
dfs(x,graph,visited); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment