Fastrail08

Accounts Merge (DSU) (Initial Approach & Hashmap Optimised approach)

Aug 28th, 2025
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class DS{
  5.     public:
  6.     int cc;
  7.     vector<int> parent, size;
  8.    
  9.     DS(){}
  10.    
  11.     DS(int n){
  12.         this-> cc = n;
  13.         this-> parent = vector<int>(n + 1);
  14.         for(int i = 0; i <= n; i++){
  15.             parent[i] = i;
  16.         }
  17.         this-> size = vector<int>(n + 1, 1);
  18.     }
  19.    
  20.    
  21.     int find(int u){
  22.         if(u == parent[u]){
  23.             return u;
  24.         }
  25.         return parent[u] = find(parent[u]);
  26.     }
  27.    
  28.     void Union(int u, int v){
  29.         int a = find(u), b = find(v);
  30.         if(a != b){
  31.             if(size[a] >= size[b]){
  32.                 parent[b] = a;
  33.             }
  34.             else{
  35.                 parent[a] = b;
  36.             }
  37.             cc--;
  38.         }
  39.     }
  40.    
  41. };
  42.  
  43. vector<vector<string> > mergeDetails(vector<vector<string> > &details){
  44.     int n = details.size();
  45.     DS dsu(n);
  46.     for(int i = 0; i < n; i++){
  47.         for(int j = i + 1; j < n; j++){
  48.             // if(details[i][0] == details[j][0]){
  49.                 for(int ik = 1; ik < details[i].size(); ik++){
  50.                     bool edgeFound = false;
  51.                     for(int jk = 1; jk < details[j].size(); jk++){
  52.                         if(details[i][ik] == details[j][jk]){
  53.                             dsu.Union(i, j);
  54.                             edgeFound = true;
  55.                             break;
  56.                         }
  57.                     }
  58.                     if(edgeFound){
  59.                         break;
  60.                     }
  61.                 }
  62.             // }
  63.         }
  64.     }
  65.    
  66.     unordered_map<int, unordered_set<string> > res;
  67.     for(int i = 0; i < details.size(); i++){
  68.         int p = dsu.find(i);
  69.         for(int j = 1; j < details[i].size(); j++){
  70.                 res[p].insert(details[i][j]);
  71.         }
  72.        
  73.     }
  74.    
  75.     vector<vector<string> > ans;
  76.     for(auto p : res){
  77.         vector<string> entry;
  78.         string name = details[p.first][0];
  79.         entry.push_back(name);
  80.         for(auto val : p.second){
  81.             entry.push_back(val);
  82.         }
  83.         sort(entry.begin() + 1, entry.end());
  84.         ans.emplace_back(entry);
  85.     }
  86.     return ans;
  87. }
  88.  
  89. //OPTMISED APPROACH(O(N*M*alpha) + N*M*log(M) + N*M) = O(N*M*logM)
  90. //Instead of checking pair wise, because of repetitive calculation, think of hashmaps in these situation. TRACK THROUGH HASHMAPS
  91. vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
  92.         int n = accounts.size();
  93.         DS dsu(n);
  94.         unordered_map<string, int> freq;
  95.         for(int i = 0; i < accounts.size(); i++){
  96.             for(int j = 1; j < accounts[i].size(); j++){
  97.                 string email = accounts[i][j];
  98.    
  99.                 //Already an user that has the same email
  100.                 //If 2 users share atleast 1 common mail, they are the same user
  101.                 //Add Edge between them
  102.                 if(freq.count(email) > 0){
  103.                     int prevUser = freq[email];
  104.                     dsu.Union(prevUser, i);
  105.                 }
  106.                 else{
  107.                     freq[email] = i;
  108.                 }
  109.    
  110.             }
  111.         }
  112.        
  113.         //Used set to sort the emails while they are getting inserted
  114.         unordered_map<int, set<string> > users;
  115.        
  116.         for(int i = 0; i < n; i++){
  117.             int p = dsu.find(i);
  118.             for(int j = 1; j < accounts[i].size(); j++){
  119.                 users[p].insert(accounts[i][j]);
  120.             }
  121.         }
  122.    
  123.         vector<vector<string> > ans;
  124.    
  125.         for(auto user : users){
  126.             string name = accounts[user.first][0];
  127.             vector<string> entry;
  128.             entry.push_back(name);
  129.             for(string s : user.second){
  130.                 entry.push_back(s);
  131.             }
  132.             ans.emplace_back(entry);
  133.         }
  134.     return ans;
  135. }
  136.  
  137. int main() {
  138.     // your code goes here
  139.     int n;
  140.     cin >> n;
  141.     vector<vector<string> > details(n);
  142.     for(int i = 0; i < n; i++){
  143.         string s;
  144.         cin >> s;
  145.         details[i].push_back(s);
  146.         int m;
  147.         cin >> m;
  148.         for(int j = 0; j < m; j++){
  149.             cin >> s;
  150.             details[i].push_back(s);
  151.         }
  152.     }
  153.     vector<vector<string> > ans = accountsMerge(details);
  154.     for(auto v : ans){
  155.         for(string s : v){
  156.             cout << s << " ";
  157.         }
  158.         cout << '\n';
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment