SAC '21 Code Challenge P5 - Friends of Friends
View as PDFAfter a student started a rumour, you have been tasked with implementing a system to count the number of people that heard it for the  students in the school: you will be asked 
 queries of 
 types:
1 u v:  and 
 are now friends and can spread the rumour from their friends to each other.
2 x: If  started a rumour, output the number of people that heard the rumour, including the starter.
Input Specification
The first line will contain  and 
, the number of students and the number of queries.
The next  lines will contain one of the above queries.
Output Specification
Output the answer to each type  query.
Constraints
For all subtasks:
Subtask 1 [40%]
Subtask 2 [60%]
No additional constraints.
Sample Input
5 6
2 3
1 1 2
1 2 3
2 1
1 4 5
2 4
Sample Output
1
3
2
Explanation for Sample Output
For the  query, the 
 student is only able to reach himself, so the answer is 
. For the 
 query, the 
 student can reach himself and the 
 and 
 student. For the 
 query, the 
 person can only reach himself and the 
 student.
Comments
My DFS TLEd on the 3rd batch :(
DFS will most likely TLE for this problem. Try using another data structure. You can also check the editorial for more tips.