leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

2685.cpp (743B)


      1 class Solution {
      2   public:
      3     int countCompleteComponents(int n, const vector<vector<int>> &edges) {
      4         static uint64_t con[50];
      5 
      6         for (uint64_t i = 0; i < n; i++)
      7             con[i] = 1ll << i;
      8         for (const auto &edge : edges) {
      9             con[edge[0]] |= 1ll << edge[1];
     10             con[edge[1]] |= 1ll << edge[0];
     11         }
     12 
     13         int res = 0;
     14         for (int i = 0; i < n; i++) {
     15             if (!con[i]) continue;
     16             for (uint64_t crnt = con[i] ^ (1ll << i), idx; crnt; crnt ^= 1ll << idx) {
     17                 idx = __builtin_ctzll(crnt);
     18                 if (con[idx] != con[i]) goto next;
     19                 con[idx] = 0;
     20             }
     21             res++;
     22         next:;
     23         }
     24 
     25         return res;
     26     }
     27 };