text: C # implementation of the approach
code: using System ; using System . Collections . Generic ; class GFG { static int MAX = 100005 ; static List < List < int > > adjacent = new List < List < int > > ( ) ; static bool [ ] visited = new bool [ MAX ] ;