-
Notifications
You must be signed in to change notification settings - Fork 0
/
15may.txt
43 lines (40 loc) · 1.59 KB
/
15may.txt
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
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>> &accounts) {
// code here
unordered_map<string, string> emailToName;
unordered_map<string, unordered_set<string>> graph;
for (auto& account : accounts) {
string name = account[0];
for (int i = 1; i < account.size(); ++i) {
emailToName[account[i]] = name;
graph[account[i]].insert(account[1]);
graph[account[1]].insert(account[i]);
}
}
vector<vector<string>> mergedAccounts;
unordered_set<string> visited;
for (auto it = graph.begin(); it != graph.end(); ++it) {
const string& email = it->first;
const unordered_set<string>& neighbors = it->second;
if (!visited.count(email)) {
vector<string> component;
dfs(email, graph, visited, component);
sort(component.begin(), component.end());
component.insert(component.begin(), emailToName[email]);
mergedAccounts.push_back(component);
}
}
return mergedAccounts;
}
private:
void dfs(const string& email, unordered_map<string, unordered_set<string>>& graph, unordered_set<string>& visited, vector<string>& component) {
visited.insert(email);
component.push_back(email);
for (const auto& neighbor : graph[email]) {
if (!visited.count(neighbor)) {
dfs(neighbor, graph, visited, component);
}
}
}
};