Moat - Atcoder Beginner Contest 219

I was unable to solve this problem during the contest. Later I read the editorial and watched some succesful submissions, there I noticed that htey did it using brute force approach but all of them used bit mask and dfs and I didn’t understand any code, considering I am very new in competitve programming, can someone please explain the approach to code for this problem?