DSU rejected why?
Can someone please guide me with this?\nI tried to detect cycle in the graph and then get most popular language amongst them.\nThen summition of abs(totalSizeOf
Can someone please guide me with this?\nI tried to detect cycle in the graph and then get most popular language amongst them.\nThen summition of abs(totalSizeOfConnectedComponents-frequencyOfUserKnowing a langauage) gives me answer\n```\n\n#define REP(i,a,b) for(int i=a;i<b;i++)\n\nclass Solution {\npublic:\n    \n    typedef struct node{\n        set<int>allNodes;\n        struct node *next;\n    }node;\n    \n    unordered_map<int,struct node *>dic;\n    unordered_map<struct node *,bool>parentDic;\n    \n    void init(int x){\n        node *newNode=new node;\n        newNode->next=NULL;\n        newNode->allNodes.insert(x);\n        dic[x]=newNode;\n        \n        parentDic[newNode]=true;\n        //cout<<newNode<<endl;\n    }\n    \n    node *parent(int d){\n        node *p=dic[d];\n        cout<<"p :"<<p<<endl;\n        while(p->next!=NULL)p=p->next;\n        node *q=dic[d];\n        while(q->next!=p&&q->next!=NULL){\n            node *t=q->next;\n            q->next=p;\n            q=t;\n        }\n        cout<<p<<endl;\n        return p;\n    }\n    \n    void merge(int a,int b){\n        \n        node *parentA=parent(a);\n        node *parentB=parent(b);\n        \n        if(parentA!=parentB){\n            \n            parentA->next=parentB;\n            \n            for(auto ele:parentA->allNodes){\n                cout<<"inserting :"<<ele<<endl;\n                parentB->allNodes.insert(ele);\n            }\n            \n            parentDic[parentA]=false;\n            parentDic[parentB]=true;\n            \n        }\n        \n    }\n    \n    \n    \n    int minimumTeachings(int n, vector<vector<int>>& languages, vector<vector<int>>& friendships) {\n        \n        int users=languages.size();\n        int i;\n        REP(i,0,users){\n            init(i);\n        }\n        \n        for(auto ele:friendships){\n            int s=ele[0];\n            int d=ele[1];\n            s--;\n            d--;\n            merge(s,d);\n        }\n        \n        int sum=0;\n        for(auto ele:parentDic){\n            if(ele.second==true){\n                \n                unordered_map<int,int>mostLang;\n                \n                int size=ele.first->allNodes.size();\n                \n                if(size>1){\n                    for(auto every:ele.first->allNodes){\n                    for(auto eachLan:languages[every]){\n                        mostLang[eachLan]++;\n                    }\n                }\n                \n                int mini=0;\n                \n                for(auto evry:mostLang){\n                    mini=max(mini,evry.second);\n                }\n                \n                sum+=abs(mini-size);\n                    \n                    \n                }\n                \n                \n            }\n        }\n        \n        return sum;\n        \n    }\n};\n\n\n```
[]