文章目录
并查集(方法一)
cpp
class Solution {
unordered_map<string, int> index; // 每个邮箱都有一个唯一编号
int root[10010]; // 并查集
void unionset(int a, int b) {
int rt1 = findrt(a), rt2 = findrt(b);
if (rt1 == rt2) return; // 很重要
root[rt1] += root[rt2];
root[rt2] = rt1;
}
int findrt(int a) {
if (root[a] < 0) return a;
return root[a] = findrt(root[a]);
}
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
memset(root, -1, sizeof(root));
// 给出index
for (int i = 0; i < accounts.size(); i++) {
for (int j = 1; j < accounts[i].size(); j++) {
index[accounts[i][j]]++;
}
}
int email_cnt = 0;
for (auto it = index.begin(); it != index.end(); it++) {
it->second = email_cnt++;
}
// 并
for (int i = 0; i < accounts.size(); i++) {
for (int j = 2; j < accounts[i].size(); j++) {
unionset(index[accounts[i][j - 1]], index[accounts[i][j]]);
}
}
// 查
unordered_map<int, int> rt_and_ansIndex;
vector<vector<string>> ans;
for (int i = 0; i < accounts.size(); i++) {
int rt_index = findrt(index[accounts[i][1]]);
if (rt_and_ansIndex.count(rt_index) == 0) {
rt_and_ansIndex[rt_index] = ans.size();
ans.push_back(vector<string>());
ans.back().push_back(accounts[i][0]); // 账户名
}
for (int j = 1; j < accounts[i].size(); j++) {
ans[rt_and_ansIndex[rt_index]].push_back(accounts[i][j]); // 邮箱
}
}
for (int i = 0; i < ans.size(); i++) {
sort(ans[i].begin() + 1, ans[i].end()); // 排序
ans[i].erase(unique(ans[i].begin() + 1, ans[i].end()), ans[i].end()); // 去重
}
return ans;
}
};
dfs(方法二)
cpp
class Solution {
unordered_map<string, int> index; // 每个邮箱都有一个唯一编号
string emails[10010];
vector<vector<int>> g; // 图,邻接表存法
bool visited[10010];
vector<vector<string>> ans;
void dfs(int n) {
visited[n] = true;
ans.back().push_back(emails[n]);
for (int i = 0; i < g[n].size(); i++) {
if (!visited[g[n][i]]) {
dfs(g[n][i]);
}
}
return;
}
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
memset(visited, false, sizeof(visited));
// 给出index
for (int i = 0; i < accounts.size(); i++) {
for (int j = 1; j < accounts[i].size(); j++) {
index[accounts[i][j]]++;
}
}
int email_cnt = 0;
for (auto it = index.begin(); it != index.end(); it++) {
it->second = email_cnt++;
emails[it->second] = it->first;
}
g.resize(email_cnt);
// 建图
for (int i = 0; i < accounts.size(); i++) {
for (int j = 2; j < accounts[i].size(); j++) {
g[index[accounts[i][1]]].push_back(index[accounts[i][j]]);
g[index[accounts[i][j]]].push_back(index[accounts[i][1]]); //双向边
}
}
// dfs
for (int i = 0; i < accounts.size(); i++) {
if (!visited[index[accounts[i][1]]]) {
ans.push_back(vector<string>());
ans.back().push_back(accounts[i][0]);
dfs(index[accounts[i][1]]);
}
}
for (int i = 0; i < ans.size(); i++) {
sort(ans[i].begin() + 1, ans[i].end()); // 排序
ans[i].erase(unique(ans[i].begin() + 1, ans[i].end()),
ans[i].end()); // 去重
}
return ans;
}
};
dfs换一种写法
cpp
class Solution {
unordered_map<string, vector<string>> g; // 图,邻接表存法,存邮箱,而不是
set<string> visited;
vector<vector<string>> ans;
void dfs(string str) {
visited.insert(str);
ans.back().push_back(str);
for (int i = 0; i < g[str].size(); i++) {
if (visited.count(g[str][i]) == 0) {
dfs(g[str][i]);
}
}
return;
}
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
// 建图
for (int i = 0; i < accounts.size(); i++) {
for (int j = 2; j < accounts[i].size(); j++) {
g[accounts[i][1]].push_back(accounts[i][j]);
g[accounts[i][j]].push_back(accounts[i][1]);
}
}
// dfs
for (int i = 0; i < accounts.size(); i++) {
if (visited.count(accounts[i][1]) == 0) {
ans.push_back(vector<string>());
ans.back().push_back(accounts[i][0]);
dfs(accounts[i][1]);
}
}
for (int i = 0; i < ans.size(); i++) {
sort(ans[i].begin() + 1, ans[i].end()); // 排序
ans[i].erase(unique(ans[i].begin() + 1, ans[i].end()), ans[i].end()); // 去重
}
return ans;
}
};
如果unordered_map改为map,更慢。