Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void dfs (int curr, vector<vector<int>> graph, vector<int> visited, unordered_map<int, string> hash_id_to_name){
  5.   //Print email or nickname of one person (To print only nickname, the hash could has an addional information
  6.   //in the value, that can be a boolean that says if it is one or other.
  7.   cout << hash_id_to_name[curr] << '\n';
  8.   for (int i = 0; i < graph[curr].size(); i++){
  9.     int adj = graph[curr][i];
  10.     if (!visited[adj]){
  11.       visited[adj] = true;
  12.       dfs(adj, graph, visited, hash_id_to_name);
  13.     }
  14.   }
  15. }
  16.  
  17. //receives a vector such that arr[i] = NickName, email1, email2, email3 ... emailN
  18. int getGroups (vector< vector<strings> > arr_of_names) {
  19.   vector< vector<int> > graph ;
  20.   //given an email or a nickname as key, return a unic id integer representing that email/nickname
  21.   unordered_map<string, int> hash_name_to_id;
  22.   //opposite to the hash above
  23.   unordered_map<int, string> hash_id_to_name;
  24.   int next_id = 0;
  25.  
  26.   for (auto list_of_emails : arr_of_names){
  27.     hash_name_to_id.insert(pair<string,int>(list_of_emails[0], next_id));
  28.     hash_id_to_name.insert(pair<int,string>(next_id, list_of_emails[0]));
  29.     graph.push_back(vector<int>());
  30.     int idx_nickname = next_id ++;
  31.    
  32.     for (int i = 1; i < list_emails.size(); i++){
  33.       string email = list_emails[i] ;
  34.       int idx_email;
  35.      
  36.       //verify if this email has appeard
  37.       auto it = hash_name_to_id.find(email);
  38.       if (it == hash_name_to_id.end()){
  39.         hash_name_to_id.insert(pair<string,int>(email, next_id));
  40.         hash_id_to_name.insert(pair<int,string>(next_id, email));
  41.         graph.push_back(vector<int>());
  42.         idx_email = next_idx ++;
  43.       }else
  44.         idx_email = it->second;
  45.      
  46.       //put the new edges in the graph
  47.       graph[idx_email].push_back(idx_nickname);
  48.       graph[idx_nickname].push_back(idx_email);
  49.     }
  50.   }
  51.  
  52.   vector<bool> visited(next_id, 0);
  53.   int cnt_people = 0;
  54.   for (int i = 0; i < next_id; i++){
  55.     //it is not visited
  56.     if (!visited[i]){
  57.       //Print vertex of one group
  58.       dfs (i);
  59.       cnt_people ++;
  60.     }
  61.   }
  62. }
  63.  
  64. // To execute C++, please define "int main()"
  65. int main() {
  66.  
  67.  
  68.   return 0;
  69. }
  70.  
  71. /*
  72.  
  73. First approach:
  74.  
  75. map<string, int>
  76. (email, unic_id)
  77.  
  78. map<int, string>
  79. (unic_id, email)
  80.  
  81. */
  82. /*
  83. Input:
  84.  
  85. Site1:
  86. Ana-dias: ana.dias@yahuu.com, ana-d@yahuu.com
  87. ONinja: rui@coldmail.com, oNinja@tmail.com
  88.  
  89. Site2:
  90. Rui42: rui.r@yahuu.com, rui@coldmail.com
  91. Bbia: bia@coldmail.com, bb@coldmail.com
  92.  
  93. Site3:
  94. Rui.Dias40: rui.r@yahuu.com
  95. PedroA: pedro@yahuu.com, pedrinho@coldmail.com
  96. Gil.P: gil.p@tmail.com
  97.  
  98. Site4:
  99. Ana97: ana.dias@yahuu.com
  100. Rui.Silva: rui.silve@coldmail.com
  101. Ninja.7: oNinja@tmail.com
  102.  
  103.  
  104. ana.dias@yahuu.com <-> Ana-dias, Ana97
  105. ana.d@tmail.com <-> Ana-dias
  106.  
  107. rui.r@yahuu.com <-> Rui42, rui@coldmail.com, Rui.Dias40, ONinja, oNinja@tmail.com
  108. oNinja@tmail.com <-> ONinja, Ninja.7
  109.  
  110.  
  111. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement