Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class DS{
- public:
- int cc;
- vector<int> parent, size;
- DS(){}
- DS(int n){
- this-> cc = n;
- this-> parent = vector<int>(n + 1);
- for(int i = 0; i <= n; i++){
- parent[i] = i;
- }
- this-> size = vector<int>(n + 1, 1);
- }
- int find(int u){
- if(u == parent[u]){
- return u;
- }
- return parent[u] = find(parent[u]);
- }
- void Union(int u, int v){
- int a = find(u), b = find(v);
- if(a != b){
- if(size[a] >= size[b]){
- parent[b] = a;
- }
- else{
- parent[a] = b;
- }
- cc--;
- }
- }
- };
- vector<vector<string> > mergeDetails(vector<vector<string> > &details){
- int n = details.size();
- DS dsu(n);
- for(int i = 0; i < n; i++){
- for(int j = i + 1; j < n; j++){
- // if(details[i][0] == details[j][0]){
- for(int ik = 1; ik < details[i].size(); ik++){
- bool edgeFound = false;
- for(int jk = 1; jk < details[j].size(); jk++){
- if(details[i][ik] == details[j][jk]){
- dsu.Union(i, j);
- edgeFound = true;
- break;
- }
- }
- if(edgeFound){
- break;
- }
- }
- // }
- }
- }
- unordered_map<int, unordered_set<string> > res;
- for(int i = 0; i < details.size(); i++){
- int p = dsu.find(i);
- for(int j = 1; j < details[i].size(); j++){
- res[p].insert(details[i][j]);
- }
- }
- vector<vector<string> > ans;
- for(auto p : res){
- vector<string> entry;
- string name = details[p.first][0];
- entry.push_back(name);
- for(auto val : p.second){
- entry.push_back(val);
- }
- sort(entry.begin() + 1, entry.end());
- ans.emplace_back(entry);
- }
- return ans;
- }
- //OPTMISED APPROACH(O(N*M*alpha) + N*M*log(M) + N*M) = O(N*M*logM)
- //Instead of checking pair wise, because of repetitive calculation, think of hashmaps in these situation. TRACK THROUGH HASHMAPS
- vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
- int n = accounts.size();
- DS dsu(n);
- unordered_map<string, int> freq;
- for(int i = 0; i < accounts.size(); i++){
- for(int j = 1; j < accounts[i].size(); j++){
- string email = accounts[i][j];
- //Already an user that has the same email
- //If 2 users share atleast 1 common mail, they are the same user
- //Add Edge between them
- if(freq.count(email) > 0){
- int prevUser = freq[email];
- dsu.Union(prevUser, i);
- }
- else{
- freq[email] = i;
- }
- }
- }
- //Used set to sort the emails while they are getting inserted
- unordered_map<int, set<string> > users;
- for(int i = 0; i < n; i++){
- int p = dsu.find(i);
- for(int j = 1; j < accounts[i].size(); j++){
- users[p].insert(accounts[i][j]);
- }
- }
- vector<vector<string> > ans;
- for(auto user : users){
- string name = accounts[user.first][0];
- vector<string> entry;
- entry.push_back(name);
- for(string s : user.second){
- entry.push_back(s);
- }
- ans.emplace_back(entry);
- }
- return ans;
- }
- int main() {
- // your code goes here
- int n;
- cin >> n;
- vector<vector<string> > details(n);
- for(int i = 0; i < n; i++){
- string s;
- cin >> s;
- details[i].push_back(s);
- int m;
- cin >> m;
- for(int j = 0; j < m; j++){
- cin >> s;
- details[i].push_back(s);
- }
- }
- vector<vector<string> > ans = accountsMerge(details);
- for(auto v : ans){
- for(string s : v){
- cout << s << " ";
- }
- cout << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment