Fastrail08

Largest Divisible Subset PRINT VERSION (TOPOLOGICAL SORT)

Sep 12th, 2025
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void formPath(int node, int length, vector<int> &path, vector<int> &res, vector<int> &arr, vector<unordered_set<int> > &preds){
  5.     //ultimate parent
  6.     if(preds[node].size() == 0 && length == 1){
  7.         // cout << path << '\n';
  8.         path.push_back(arr[node]);
  9.         vector<int> comparePath = path;
  10.         reverse(comparePath.begin(), comparePath.end());
  11.         if(res < comparePath){
  12.             res = comparePath;
  13.         }
  14.         path.pop_back();
  15.         return;
  16.     }
  17.     path.push_back(arr[node]);
  18.     for(int i : preds[node]){
  19.         int parent = i;
  20.         formPath(parent, length - 1, path, res, arr, preds);
  21.     }
  22.     path.pop_back();
  23. }
  24.  
  25. vector<int> formPathOptimised(int node, vector<int> &arr, vector<int> &parent){
  26.     if(node == parent[node]){
  27.         vector<int> base = {arr[node]};
  28.         return base;
  29.     }
  30.     vector<int> partialPath = formPathOptimised(parent[node], arr, parent);
  31.     partialPath.push_back(arr[node]);
  32.     return partialPath;
  33. }
  34.  
  35. vector<int> kahn(vector<int> &arr, vector<vector<int> > &adj){
  36.     queue<int> q;
  37.     vector<int> indegree(adj.size(), 0);
  38.     vector<int> order;
  39.    
  40.     for(int i = 0; i < adj.size(); i++){
  41.         for(int j = 0; j < adj[i].size(); j++){
  42.             int node = adj[i][j];
  43.             indegree[node]++;
  44.         }
  45.     }
  46.    
  47.     for(int i = 0; i < indegree.size(); i++){
  48.         if(indegree[i] == 0){
  49.             q.push(i);
  50.         }
  51.     }
  52.    
  53.     while(!q.empty()){
  54.         int front = q.front();
  55.         q.pop();
  56.         order.push_back(front);
  57.         for(int i = 0; i < adj[front].size(); i++){
  58.             int node = adj[front][i];
  59.             indegree[node]--;
  60.             if(indegree[node] == 0){
  61.                 q.push(node);
  62.             }
  63.         }
  64.     }
  65.    
  66.     //Initially all the nodes are valid sequences of length 1
  67.     // {2}, {17} => all these sequences are of length 1 and are valid divisible subset
  68.     vector<int> length(order.size(), 1);
  69.     vector<unordered_set<int> > preds(order.size());
  70.     vector<int> parent(order.size());
  71.     for(int i = 0; i < parent.size(); i++){
  72.         parent[i] = i;
  73.     }
  74.     //maxLength would ATLEAST BE 1
  75.     //If you KEEP maxLength = 0, but if all the nodes are independent of each other,
  76.     //then the relaxation loop will never run and maxLength would be returned as 0, which is not true. A single element is always valid, and it is valid sequence of size 1
  77.     int maxLength = 1;
  78.     for(int k = 0; k < order.size(); k++){
  79.         int node = order[k];
  80.        
  81.         for(int i = 0; i < adj[node].size(); i++){
  82.             int child = adj[node][i];
  83.             // preds[child].insert(node);
  84.             if(length[child] < length[node] + 1){
  85.                 parent[child] = node;
  86.             }
  87.             //IMPORTANT - DON'T SAVE EVERY PARENT, ONLY WHICH IS THE BEST OUT OF ALL PARENTS
  88.             //If same length/distance for the child is achieved from a different parent, store the parent that is lexicographically better, here lexicographically larger.
  89.             else if(length[child] == length[node] + 1){
  90.                 int oldParent = arr[parent[child]], newParent = arr[node];
  91.                 vector<int> chain1 = formPathOptimised(parent[child], arr, parent);
  92.                 chain1.push_back(arr[child]);
  93.                 vector<int> chain2 = formPathOptimised(node, arr, parent);
  94.                 chain2.push_back(arr[child]);
  95.                 //lexicographic comparison
  96.                 if(chain1 < chain2){
  97.                     parent[child] = node;
  98.                 }
  99.             }
  100.             length[child] = max(length[child], length[node] + 1);
  101.             maxLength = max(maxLength, length[child]);
  102.         }
  103.     }
  104.     vector<int> lexRes;
  105.     for(int i = 0; i < length.size(); i++){
  106.         if(length[i] == maxLength){
  107.             vector<int> res = formPathOptimised(i, arr, parent);
  108.             //best path which is the longest and lexicographically largest
  109.             if(lexRes < res){
  110.                 lexRes = res;
  111.             }
  112.         }
  113.     }
  114.     // for(int i = 0; i < length.size(); i++){
  115.     //     //Only create path if the node belongs to LIS (size of chain = longest sequence)
  116.     //     if(length[i] == maxLength){
  117.     //         vector<int> path, res;
  118.     //         formPath(i, maxLength, path, res, arr, preds);
  119.     //         if(res > lexRes){
  120.     //             lexRes = res;
  121.     //         }
  122.     //     }
  123.     // }
  124.     // cout << lexRes << '\n';
  125.     // stringstream ss(lexRes);
  126.     // vector<int> res;
  127.     // string segment;
  128.     // while(getline(ss, segment, ',')){
  129.     //     int idx = stoi(segment);
  130.     //     res.push_back(arr[idx]);
  131.     // }
  132.     return lexRes;
  133. }
  134.  
  135. vector<int> largestSubset(vector<int> &arr){
  136.     vector<vector<int> > adj(arr.size(), vector<int>());
  137.     sort(arr.begin(), arr.end());
  138.     for(int i = 0; i < arr.size(); i++){
  139.         for(int j = i + 1; j < arr.size(); j++){
  140.             //arr[i] | arr[j]
  141.             //arr[i] -------> arr[j] (arr[i] is prerequisite of arr[j])
  142.             if(arr[j] % arr[i] == 0){
  143.                 adj[i].push_back(j);
  144.             }
  145.         }
  146.     }
  147.    
  148.     return kahn(arr, adj);
  149.    
  150. }
  151.  
  152. int main() {
  153.     // your code goes here
  154.     int n;
  155.     cin >> n;
  156.     vector<int> arr(n);
  157.     for(int i = 0; i < n; i++){
  158.         cin >> arr[i];
  159.     }
  160.     vector<int> res = largestSubset(arr);
  161.     for(int i : res){
  162.         cout << i << " ";
  163.     }
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment