-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path721+Accounts Merge.cpp
More file actions
65 lines (53 loc) · 1.8 KB
/
Copy path721+Accounts Merge.cpp
File metadata and controls
65 lines (53 loc) · 1.8 KB
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
class Solution {
public:
struct UF {
unordered_map<string, string> fa;
string find(string x) {
if (x == fa[x]) return x;
else {
string F = find(fa[x]);
fa[x] = F;
return F;
}
}
void merge(string x, string y) { fa[find(y)] = find(x); }
} uf;
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
/*init*/
unordered_map<string, string> umap;
for(auto account : accounts) {
for (int i = 1; i < account.size(); ++i) {
uf.fa[account[i]] = account[i];
umap[account[i]] = account[0];
}
}
for (auto& account : accounts) {
int size = account.size();
if (size == 2) continue;
sort(account.begin()+1, account.end());
for (int i = 1; i < account.size(); ++i) {
uf.merge(account[1], account[i]);
}
}
unordered_map<string, vector<string>> resumap;
vector<vector<string>> res;
for (auto ump : umap) {
string mail = ump.first;
string name = ump.second;
string fa = uf.find(mail);
resumap[fa].push_back(mail);
}
for (auto resump : resumap) {
vector<string> resT;
sort(resump.second.begin(), resump.second.end());
for (int i = 0; i < resump.second.size(); ++i) {
if (i == 0) {
resT.push_back(umap[resump.second[i]]);
}
resT.push_back(resump.second[i]);
}
res.push_back(resT);
}
return res;
}
};