Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void formPath(int node, int length, vector<int> &path, vector<int> &res, vector<int> &arr, vector<unordered_set<int> > &preds){
- //ultimate parent
- if(preds[node].size() == 0 && length == 1){
- // cout << path << '\n';
- path.push_back(arr[node]);
- vector<int> comparePath = path;
- reverse(comparePath.begin(), comparePath.end());
- if(res < comparePath){
- res = comparePath;
- }
- path.pop_back();
- return;
- }
- path.push_back(arr[node]);
- for(int i : preds[node]){
- int parent = i;
- formPath(parent, length - 1, path, res, arr, preds);
- }
- path.pop_back();
- }
- vector<int> formPathOptimised(int node, vector<int> &arr, vector<int> &parent){
- if(node == parent[node]){
- vector<int> base = {arr[node]};
- return base;
- }
- vector<int> partialPath = formPathOptimised(parent[node], arr, parent);
- partialPath.push_back(arr[node]);
- return partialPath;
- }
- vector<int> kahn(vector<int> &arr, vector<vector<int> > &adj){
- queue<int> q;
- vector<int> indegree(adj.size(), 0);
- vector<int> order;
- for(int i = 0; i < adj.size(); i++){
- for(int j = 0; j < adj[i].size(); j++){
- int node = adj[i][j];
- indegree[node]++;
- }
- }
- for(int i = 0; i < indegree.size(); i++){
- if(indegree[i] == 0){
- q.push(i);
- }
- }
- while(!q.empty()){
- int front = q.front();
- q.pop();
- order.push_back(front);
- for(int i = 0; i < adj[front].size(); i++){
- int node = adj[front][i];
- indegree[node]--;
- if(indegree[node] == 0){
- q.push(node);
- }
- }
- }
- //Initially all the nodes are valid sequences of length 1
- // {2}, {17} => all these sequences are of length 1 and are valid divisible subset
- vector<int> length(order.size(), 1);
- vector<unordered_set<int> > preds(order.size());
- vector<int> parent(order.size());
- for(int i = 0; i < parent.size(); i++){
- parent[i] = i;
- }
- //maxLength would ATLEAST BE 1
- //If you KEEP maxLength = 0, but if all the nodes are independent of each other,
- //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
- int maxLength = 1;
- for(int k = 0; k < order.size(); k++){
- int node = order[k];
- for(int i = 0; i < adj[node].size(); i++){
- int child = adj[node][i];
- // preds[child].insert(node);
- if(length[child] < length[node] + 1){
- parent[child] = node;
- }
- //IMPORTANT - DON'T SAVE EVERY PARENT, ONLY WHICH IS THE BEST OUT OF ALL PARENTS
- //If same length/distance for the child is achieved from a different parent, store the parent that is lexicographically better, here lexicographically larger.
- else if(length[child] == length[node] + 1){
- int oldParent = arr[parent[child]], newParent = arr[node];
- vector<int> chain1 = formPathOptimised(parent[child], arr, parent);
- chain1.push_back(arr[child]);
- vector<int> chain2 = formPathOptimised(node, arr, parent);
- chain2.push_back(arr[child]);
- //lexicographic comparison
- if(chain1 < chain2){
- parent[child] = node;
- }
- }
- length[child] = max(length[child], length[node] + 1);
- maxLength = max(maxLength, length[child]);
- }
- }
- vector<int> lexRes;
- for(int i = 0; i < length.size(); i++){
- if(length[i] == maxLength){
- vector<int> res = formPathOptimised(i, arr, parent);
- //best path which is the longest and lexicographically largest
- if(lexRes < res){
- lexRes = res;
- }
- }
- }
- // for(int i = 0; i < length.size(); i++){
- // //Only create path if the node belongs to LIS (size of chain = longest sequence)
- // if(length[i] == maxLength){
- // vector<int> path, res;
- // formPath(i, maxLength, path, res, arr, preds);
- // if(res > lexRes){
- // lexRes = res;
- // }
- // }
- // }
- // cout << lexRes << '\n';
- // stringstream ss(lexRes);
- // vector<int> res;
- // string segment;
- // while(getline(ss, segment, ',')){
- // int idx = stoi(segment);
- // res.push_back(arr[idx]);
- // }
- return lexRes;
- }
- vector<int> largestSubset(vector<int> &arr){
- vector<vector<int> > adj(arr.size(), vector<int>());
- sort(arr.begin(), arr.end());
- for(int i = 0; i < arr.size(); i++){
- for(int j = i + 1; j < arr.size(); j++){
- //arr[i] | arr[j]
- //arr[i] -------> arr[j] (arr[i] is prerequisite of arr[j])
- if(arr[j] % arr[i] == 0){
- adj[i].push_back(j);
- }
- }
- }
- return kahn(arr, adj);
- }
- int main() {
- // your code goes here
- int n;
- cin >> n;
- vector<int> arr(n);
- for(int i = 0; i < n; i++){
- cin >> arr[i];
- }
- vector<int> res = largestSubset(arr);
- for(int i : res){
- cout << i << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment