Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. int nonDivisibleSubset(int k, vector<int> S) {
  2.     std::map<int, std::set<int>> x;
  3.  
  4.     auto n = S.size();
  5.     for (int i = 0; i < n; i++) {
  6.         for (int j = i+1; j < n; j++) {
  7.             if ((S[i]+S[j])%k) {
  8.                 x[std::min(S[i], S[j])].insert(std::max(S[i], S[j]));
  9.             }
  10.         }
  11.     }
  12.  
  13.     std::map<int, int> tab;
  14.     std::function<int(int)> longestPath = [&](int w) {
  15.         if (tab.find(w) != tab.end()) return tab[w];
  16.         if (x.find(w) == x.end()) return tab[w] = 0;
  17.  
  18.         int _max = 0;
  19.         for (auto &it : x[w]) _max = std::max(_max, longestPath(it));
  20.  
  21.         return tab[w] = _max + 1;
  22.     };
  23.  
  24.     int _max = 0;
  25.     for (auto &it:x) _max = std::max(_max, longestPath(it.first));
  26.  
  27.     return _max + 1;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement