
Consider ‘arr’ = [(1, 2, 100), (3, 4, 200), (1, 3, 300), (2, 5, 400)]
Person 1 will tell the story to person 2 at 100th timeStamp.
Person 1 will tell the story to person 3 at 300th timeStamp.
Person 2 will tell the story to person 5 at 400th timeStamp.
Person 4 will not be able to learn the story.
So the output will be [1, 2, 3, 5].
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows.
The first line of each test case contains an integer ‘N’, denoting the number of meeting between people.
The next ‘N’ lines contain 3 space-separated integers denoting ‘person1’, ‘person2’, and ‘timeStamp’.
For each test case, print an array of integers in sorted order representing all persons who will know the story by the end.
The output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^5
1 <= person1 <= person2 <= 10^6
1 <= timeStamp <= 10^9
Time limit: 1 sec
In the approach, we will use a depth-first search to find all persons who will know the store by the end. We will maintain a visited HashMap to store the people who have been visited. We will maintain a HashMap graph to create a directed edge for the meeting between the people.
We will create a recursive function helper(graph, visited, index, time) where graph and visited is the HashMap, the index is the current person, and time is the current time.
We will maintain a queue of persons. Initially, only person 1 knows the story, so we will push it into our queue. Now we will add all those people who are reachable from person1 into the queue with their time. We have the complete information of when the person gets to know about the story. So if a person doesn’t know the story yet, they will not be able to share it with others. Hence we will check whether a person knows the story before meeting another person or not.