Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int nonDivisibleSubset(int k, vector<int> S) {
- std::map<int, std::set<int>> x;
- auto n = S.size();
- for (int i = 0; i < n; i++) {
- for (int j = i+1; j < n; j++) {
- if ((S[i]+S[j])%k) {
- x[std::min(S[i], S[j])].insert(std::max(S[i], S[j]));
- }
- }
- }
- std::map<int, int> tab;
- std::function<int(int)> longestPath = [&](int w) {
- if (tab.find(w) != tab.end()) return tab[w];
- if (x.find(w) == x.end()) return tab[w] = 0;
- int _max = 0;
- for (auto &it : x[w]) _max = std::max(_max, longestPath(it));
- return tab[w] = _max + 1;
- };
- int _max = 0;
- for (auto &it:x) _max = std::max(_max, longestPath(it.first));
- return _max + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement