Ctrl K

News Through a Chain of Calls

Hard
A group has people people numbered from 0 to people - 1. Person 0 learns some news and, at time 0, tells it to person first_friend. After that, people make calls: calls[i] = [x, y, t] means x and y talk at time t. If at least one of them knows the news when they talk, both know it afterwards. A person may take part in several calls at the same moment. The news travels instantly: someone who learns it at time t can pass it on in other calls happening at that same time t. Return the numbers of everyone who knows the news once all calls are over, in any order.
Example 1
people=5, calls=[[1, 4, 3], [2, 3, 1], [4, 2, 6]], first_friend=1 → [0, 1, 2, 4]

At first only 0 and 1 know. At time 1, people 2 and 3 talk, but neither knows yet. At time 3, person 1 tells 4, and at time 6, person 4 tells 2. Person 3 talked to 2 before 2 learned the news, so 3 never hears it.

Example 2
people=4, calls=[[2, 3, 5], [0, 2, 5]], first_friend=1 → [0, 1, 2, 3]

Both calls happen at time 5. Person 0 tells 2, who passes it on to 3 within the same moment.

Example 3
people=3, calls=[[1, 2, 2]], first_friend=2 → [0, 1, 2]
Constraints
  • 2 ≤ people ≤ 10⁵
  • 1 ≤ len(calls) ≤ 10⁵
  • calls[i] = [x, y, t], 0 ≤ x, y ≤ people - 1, x ≠ y
  • 1 ≤ t ≤ 10⁵
  • 1 ≤ first_friend ≤ people - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Extra Link in a Rooted Tree HardPartners Side by Side HardWhich Machine to Cure HardWhich Machine to Unplug HardDistances to the Ring Road Hard
loading Python…
Input
people=5, calls=[[1, 4, 3], [2, 3, 1], [4, 2, 6]], first_friend=1
Expected
[0, 1, 2, 4]

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.