-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from pes-alcoding-club/Sept2021
Fixed editorial.md
- Loading branch information
Showing
1 changed file
with
4 additions
and
4 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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
So for the first subtask, all we had to do is use DFS to check how many places can be visited by the given city after adding the roads. | ||
So for each query of type 2, it would take O(N) time complexity. | ||
For the first subtask, all we had to do is use DFS to check how many places can be visited by the given city after adding the roads. | ||
For each query of type 2, it would take O(N) time complexity. | ||
For Q queries, the total time complexity would be O(NQ) which fits the given constraints. | ||
|
||
For the second subtask, we need a faster solution. So for this, we would use a structure called DSU or disjoint set union. | ||
For the second subtask, we need a faster solution. For this, we would use a structure called DSU or disjoint set union. | ||
For queries of type 1, we would `merge` (or in other words apply the `union`) the two cities. This takes O(logN) time complexity for each query. | ||
|
||
As for query 2, we need another data structure within our DSU which stores the size (or in this case, the number of cities we can visit) for each set (or in this case, city). | ||
If we do this, we can reduce the time taken to retrieve the size to just O(1). | ||
|
||
So for Q queries our worst case time complexity would be just O(Q log N). | ||
For Q queries our worst case time complexity would be just O(Q log N). |