category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

721. Accounts Merge, DSU implementation, graph, hard

DSU approach

code sample ```cpp class DSU { public: vector parent; DSU(int n) { n += 2; parent.resize(n); for (int i = 0; i < n; i++) parent[i] = i; } int find(int n) { return (n == parent[n] ? n : parent[n] = find(parent[n])); } void unionSet(int name, int email) { name = find(name); email = find(email); parent[email] = parent[name]; } }; class Solution { public: vector<vector> accountsMerge(vector<vector>& accounts) { map<string, int> encode; DSU dsu(accounts.size() + 2); for (int i = 0; i < accounts.size(); i++) { for (int j = 1; j < accounts[i].size(); j++) { string email = accounts[i][j]; if (encode.count(email)) dsu.unionSet(i, encode[email]); else encode[email] = i; } } map<int, vector> mp; for (const auto& [emailId, encodedParent]: encode) { int id = dsu.find(encodedParent); mp[id].push_back(emailId); } vector<vector> ans; for (auto& [id, arr]: mp) { sort(arr.begin(), arr.end()); vector temp{accounts[id][0]}; for (const auto& i: arr) temp.push_back(i); ans.push_back(temp); } return ans; } }; ``` </details> ### DFS - to be updated soon.