-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount Merge.cpp
114 lines (105 loc) · 2.95 KB
/
Account Merge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class DisjointSet{
public:
vector<int> rank,parent;
DisjointSet(int n){
rank.resize(n+1,0);
parent.resize(n+1);
for(int i=0;i<=n;i++)parent[i]=i;
}
int findUpar(int node){
if(node==parent[node])return node;
return parent[node]=findUpar(parent[node]);
}
void Unionbyrank(int u,int v){
int u_v=findUpar(u);
int u_u=findUpar(v);
if(u_v==u_u)return;
if(rank[u_v]>rank[u_u])parent[u_u]=u_v;
else if(rank[u_v]<rank[u_u])parent[u_v]=u_u;
else{
parent[u_v]=u_u;
rank[u_u]++;
}
}
};
class Solution{
public:
vector<vector<string>> accountsMerge(vector<vector<string>> &accounts) {
int n=accounts.size();
unordered_map<string,int> helper;
DisjointSet ds(n);
for(int i=0;i<n;i++){
for(int j=1;j<accounts[i].size();j++){
string mail=accounts[i][j];
if(helper.find(mail)==helper.end())helper[mail]=i;
else ds.Unionbyrank(i,helper[mail]);
}
}
vector<vector<string>> ans;
vector<string> merged[n];
for(auto it:helper){
string mail=it.first;
int node=ds.findUpar(it.second);
merged[node].push_back(mail);
}
for(int i=0;i<n;i++){
if(merged[i].size()==0)continue;
sort(merged[i].begin(),merged[i].end());
vector<string> temp;
temp.push_back(accounts[i][0]);
for(auto it:merged[i]){
temp.push_back(it);
}
ans.push_back(temp);
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<vector<string>> accounts;
for (int i = 0; i < n; i++) {
vector<string> temp;
int x;
cin >> x;
for (int j = 0; j < x; j++) {
string s1;
cin >> s1;
temp.push_back(s1);
}
accounts.push_back(temp);
}
///
Solution obj;
vector<vector<string>> res = obj.accountsMerge(accounts);
sort(res.begin(), res.end());
cout << "[";
for (int i = 0; i < res.size(); ++i) {
cout << "[";
for (int j = 0; j < res[i].size(); j++) {
if (j != res[i].size() - 1)
cout << res[i][j] << ","
<< " ";
else
cout << res[i][j];
}
if (i != res.size() - 1)
cout << "], " << endl;
else
cout << "]";
}
cout << "]" << endl;
}
}
// } Driver Code Ends