This problem looks busy at first, but the accepted solution is built around one steady invariant. For 547. Friend Circles, the solution in this repository is mainly a union-find solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: union-find.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are unite, findCircleNum.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 20 ms, faster than 84.43% of C++ online submissions for Friend Circles.
02//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Friend Circles.
03class DSU{
04public:
05 vector<int> parent;
06
07 DSU(int N){
08 parent = vector<int>(N);
09 //every node's parent is itself initially
10 iota(parent.begin(), parent.end(), 0);
11 };
12
13 int find(int x){
14 if(parent[x] != x){
15 parent[x] = find(parent[x]);
16 }
17 return parent[x];
18 }
19
20 void unite(int x, int y){
21 parent[find(x)] = find(y);
22 }
23};
24
25class Solution {
26public:
27 int findCircleNum(vector<vector<int>>& M) {
28 int n = M.size();
29
30 DSU dsu(n);
31 for(int i = 0; i < n; i++){
32 for(int j = i+1; j < n; j++){
33 if(M[i][j]){
34 // cout << i << " " << j << "|";
35 dsu.unite(i, j);
36 }
37 }
38 }
39 // cout << endl;
40
41 set<int> uniqParent;
42
43 for(int i = 0; i < n; i++){
44 //we should not use dsu.parent[i] but dsu.find(i) here!
45 // uniqParent.insert(dsu.parent[i]);
46 uniqParent.insert(dsu.find(i));
47 // cout << i << "'s parent: " << dsu.parent[i] << endl;
48 }
49
50 return uniqParent.size();
51 }
52};
Cost