-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53d0ab8
commit 8deded2
Showing
4 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
import java.math.*; | ||
|
||
class Node{ | ||
int val; | ||
ArrayList<Node> neighbors; | ||
public Node(){ | ||
val = 0; | ||
neighbors = new ArrayList<>(); | ||
} | ||
|
||
public Node(int val){ | ||
this.val = val; | ||
neighbors = new ArrayList<>(); | ||
} | ||
|
||
public Node(int val, ArrayList<Node> neighbors){ | ||
this.val = val; | ||
this.neighbors = neighbors; | ||
} | ||
} | ||
|
||
class GFG{ | ||
static class FastReader{ | ||
BufferedReader br; | ||
StringTokenizer st; | ||
|
||
public FastReader(){ | ||
br = new BufferedReader(new InputStreamReader(System.in)); | ||
} | ||
|
||
String next(){ | ||
while (st == null || !st.hasMoreElements()){ | ||
try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } | ||
} | ||
return st.nextToken(); | ||
} | ||
|
||
String nextLine(){ | ||
String str = ""; | ||
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } | ||
return str; | ||
} | ||
|
||
Integer nextInt(){ | ||
return Integer.parseInt(next()); | ||
} | ||
} | ||
|
||
static ArrayList<Node> bfs(Node src){ | ||
ArrayList<Node> ans = new ArrayList<>(); | ||
HashSet<Node> visited = new HashSet<>(); | ||
Queue<Node> q = new LinkedList<>(); | ||
q.add(src); | ||
visited.add(src); | ||
|
||
while(!q.isEmpty()){ | ||
Node u = q.poll(); | ||
ans.add(u); | ||
ArrayList<Node> v = u.neighbors; | ||
for(Node x : v){ | ||
if(!visited.contains(x)){ | ||
visited.add(x); | ||
q.add(x); | ||
} | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
static boolean checkedClone(Node prev, Node new1){ | ||
ArrayList<Node> prevAns = bfs(prev); | ||
ArrayList<Node> newAns = bfs(new1); | ||
for(int i = 0; i < prevAns.size(); i++){ | ||
if(prevAns.get(i) == newAns.get(i)) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static void main(String[] args) throws IOException{ | ||
FastReader sc = new FastReader(); | ||
PrintWriter out = new PrintWriter(System.out); | ||
int t = sc.nextInt(); | ||
while(t-- > 0){ | ||
int n = sc.nextInt(); | ||
Node root = null; | ||
Node v[] = new Node[n]; | ||
for(int i = 0; i < n; i++) v[i] = new Node(i); | ||
Solution ob = new Solution(); | ||
for(int i = 0; i < n; i++){ | ||
ArrayList<Node> li = new ArrayList<>(); | ||
String arr[] = sc.nextLine().split(" "); | ||
for(String s : arr){ | ||
li.add(v[Integer.parseInt(s)]); | ||
} | ||
v[i].neighbors = li; | ||
} | ||
ArrayList<Node> prev = bfs(v[0]); | ||
Node ans = ob.cloneGraph(v[0]); | ||
ArrayList<Node> now = bfs(ans); | ||
out.println(checkedClone(v[0], ans) ? "1" : "0"); | ||
} | ||
out.flush(); | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
|
||
/* | ||
class Node{ | ||
int val; | ||
ArrayList<Node> neighbors; | ||
public Node(){ | ||
val = 0; | ||
neighbors = new ArrayList<>(); | ||
} | ||
public Node(int val){ | ||
this.val = val; | ||
neighbors = new ArrayList<>(); | ||
} | ||
public Node(int val, ArrayList<Node> neighbors){ | ||
this.val = val; | ||
this.neighbors = neighbors; | ||
} | ||
} | ||
*/ | ||
class Solution | ||
{ | ||
Node cloneGraph(Node source) | ||
{ | ||
Queue<Node> q = new LinkedList<Node>(); | ||
q.add(source); | ||
|
||
HashMap<Node,Node> hm = new HashMap<Node,Node>(); | ||
|
||
hm.put(source,new Node(source.val)); | ||
|
||
while (!q.isEmpty()) | ||
{ | ||
Node u = q.poll(); | ||
|
||
Node cloneNodeU = hm.get(u); | ||
if (u.neighbors != null) | ||
{ | ||
ArrayList<Node> v = u.neighbors; | ||
for (Node graphNode : v) | ||
{ | ||
Node cloneNodeG = hm.get(graphNode); | ||
|
||
if (cloneNodeG == null) | ||
{ | ||
q.add(graphNode); | ||
|
||
cloneNodeG = new Node(graphNode.val); | ||
hm.put(graphNode,cloneNodeG); | ||
} | ||
|
||
cloneNodeU.neighbors.add(cloneNodeG); | ||
} | ||
} | ||
} | ||
|
||
return hm.get(source); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n+m) | ||
Space complexity - O(n) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(|word|) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution | ||
{ | ||
public String firstPalindrome(String[] words) | ||
{ | ||
for (String word : words) | ||
{ | ||
if (isPalindrome(word)) | ||
return word; | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
private boolean isPalindrome(String s) | ||
{ | ||
int i = 0; | ||
int j = s.length() -1; | ||
|
||
while (i <= j) | ||
{ | ||
if (s.charAt(i) == s.charAt(j)) | ||
{ | ||
i++; | ||
j--; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |