-
Notifications
You must be signed in to change notification settings - Fork 3
/
accounts-merge.cpp
116 lines (101 loc) · 2.98 KB
/
accounts-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
115
116
//Runtime: 153 ms
class Solution {
private:
int parent[10002];
int rank[10002];
int find(int u){
if(u != parent[u]){
parent[u] = find(parent[u]);
}
return parent[u];
}
void DJunion(int x, int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
if(rank[fx] > rank[fy]){
parent[fy] = fx;
}else{
parent[fx] = fy;
}
if(rank[fx] == rank[fy]){
rank[fy]++;
}
}
}
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
for(int i=0;i<=10001;i++){
parent[i] = i;
rank[i] = 0;
}
map<string, string> emailToName;
map<string, int>emailToId;
int id = 1;
for(auto acc:accounts){
for(int i=1;i<acc.size();i++){
emailToName[acc[i]] = acc[0];
if(emailToId.find(acc[i]) == emailToId.end()){
emailToId[acc[i]] = id++;
}
DJunion(emailToId[acc[i]], emailToId[acc[1]]);
}
}
map<int, vector<string> > res;
for(auto email:emailToName){
int id = find(emailToId[email.first]);
res[id].push_back(email.first);
}
vector<vector<string> >ans;
for(auto temp:res){
vector<string> t = temp.second;
sort(t.begin(), t.end());
string name = emailToName[t[0]];
t.insert(t.begin(), name);
ans.push_back(t);
}
return ans;
}
};
//Runtime: 539 ms
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
map<string, string> emailToName;
map<string, set<string> > adj;
for (auto acc : accounts) {
for (int i = 1; i < acc.size(); i++) {
for (int j = i; j < acc.size(); j++) {
adj[acc[i]].insert(acc[j]);
adj[acc[j]].insert(acc[i]);
}
emailToName[acc[i]] = acc[0];
}
}
vector<vector<string> > res;
set<string> vis;
for (auto bin : adj) {
if (vis.find(bin.first) == vis.end()) {
queue<string> Q;
Q.push(bin.first);
vector<string> t;
vis.insert(bin.first);
while (!Q.empty()) {
auto em = Q.front();
Q.pop();
t.push_back(em);
for (auto a : adj[em]) {
if (vis.find(a) == vis.end()) {
Q.push(a);
vis.insert(a);
}
}
}
sort(t.begin(), t.end());
t.insert(t.begin(), emailToName[t[0]]);
res.push_back(t);
}
}
return res;
}
};