Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void dfs (int curr, vector<vector<int>> graph, vector<int> visited, unordered_map<int, string> hash_id_to_name){
- //Print email or nickname of one person (To print only nickname, the hash could has an addional information
- //in the value, that can be a boolean that says if it is one or other.
- cout << hash_id_to_name[curr] << '\n';
- for (int i = 0; i < graph[curr].size(); i++){
- int adj = graph[curr][i];
- if (!visited[adj]){
- visited[adj] = true;
- dfs(adj, graph, visited, hash_id_to_name);
- }
- }
- }
- //receives a vector such that arr[i] = NickName, email1, email2, email3 ... emailN
- int getGroups (vector< vector<strings> > arr_of_names) {
- vector< vector<int> > graph ;
- //given an email or a nickname as key, return a unic id integer representing that email/nickname
- unordered_map<string, int> hash_name_to_id;
- //opposite to the hash above
- unordered_map<int, string> hash_id_to_name;
- int next_id = 0;
- for (auto list_of_emails : arr_of_names){
- hash_name_to_id.insert(pair<string,int>(list_of_emails[0], next_id));
- hash_id_to_name.insert(pair<int,string>(next_id, list_of_emails[0]));
- graph.push_back(vector<int>());
- int idx_nickname = next_id ++;
- for (int i = 1; i < list_emails.size(); i++){
- string email = list_emails[i] ;
- int idx_email;
- //verify if this email has appeard
- auto it = hash_name_to_id.find(email);
- if (it == hash_name_to_id.end()){
- hash_name_to_id.insert(pair<string,int>(email, next_id));
- hash_id_to_name.insert(pair<int,string>(next_id, email));
- graph.push_back(vector<int>());
- idx_email = next_idx ++;
- }else
- idx_email = it->second;
- //put the new edges in the graph
- graph[idx_email].push_back(idx_nickname);
- graph[idx_nickname].push_back(idx_email);
- }
- }
- vector<bool> visited(next_id, 0);
- int cnt_people = 0;
- for (int i = 0; i < next_id; i++){
- //it is not visited
- if (!visited[i]){
- //Print vertex of one group
- dfs (i);
- cnt_people ++;
- }
- }
- }
- // To execute C++, please define "int main()"
- int main() {
- return 0;
- }
- /*
- First approach:
- map<string, int>
- (email, unic_id)
- map<int, string>
- (unic_id, email)
- */
- /*
- Input:
- Site1:
- Ana-dias: ana.dias@yahuu.com, ana-d@yahuu.com
- ONinja: rui@coldmail.com, oNinja@tmail.com
- Site2:
- Rui42: rui.r@yahuu.com, rui@coldmail.com
- Bbia: bia@coldmail.com, bb@coldmail.com
- Site3:
- Rui.Dias40: rui.r@yahuu.com
- PedroA: pedro@yahuu.com, pedrinho@coldmail.com
- Gil.P: gil.p@tmail.com
- Site4:
- Ana97: ana.dias@yahuu.com
- Rui.Silva: rui.silve@coldmail.com
- Ninja.7: oNinja@tmail.com
- ana.dias@yahuu.com <-> Ana-dias, Ana97
- ana.d@tmail.com <-> Ana-dias
- rui.r@yahuu.com <-> Rui42, rui@coldmail.com, Rui.Dias40, ONinja, oNinja@tmail.com
- oNinja@tmail.com <-> ONinja, Ninja.7
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement