Subject to the effect: the same nickname may not be the same person, it could be the same person, but a different nickname is certainly not the same person that Italy will follow the same personal email address linked.
Disjoint-set:
E-mail address of the corresponding disjoint-set operations, e-mail address if there is an intersection exists, the two lists is certainly attributable to the same person, to connect them together.
1 class Solution { 2 public: 3 int find(int idr, vector<int>& u) { 4 while (idr != u[idr]) idr = u[idr]; 5 return idr; 6 } 7 void join(int idr1, int idr2, vector<int>& u) { 8 int f1 = find(idr1, u); 9 int f2 = find(idr2, u); 10 u[f2] = f1; 11 } 12 map<string, int> hashmap1; 13 map<int, vector<vector<string>>> hashmap2; 14 vector<vector<string>> res; //返回向量 15 vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) { 16 vector<int> u(accounts.size()); 17 for (int i = 0; i < u.size(); i++) u[i] = i;18initialize disjoint-set// for ( int K = 0 ; K <accounts.size (); K ++) { // find the logical connection . 19 for ( int I = . 1 ; I <Accounts [K] .size (); I ++ ) { 20 is Auto addr = Accounts [K] [I]; 21 is Auto ITER = hashmap1.find (addr); 22 is IF (ITER hashmap1.end == ()) hashmap1 [addr] = K; 23 is the else the Join (hashmap1 [addr], K, U ); 24 } 25 } 26 is 27 // print disjoint-set case 28 @for (int i = 0; i < u.size(); i++) { 29 // cout << u[i] << " "; 30 // for (auto val : accounts[i]) cout << val << " "; 31 // cout << endl; 32 //} 33 34 for (int i = 0; i < accounts.size(); i++) { //物理连接 35 int f = find(u[i], u); //对顶层节点进行分类 36 auto iter = hashmap2.find(f); 37 if (iter == hashmap2.End ()) { hashmap2 [F] =38 isif it does not exist to create a// {}; 39 } 40 hashmap2[f].push_back(accounts[i]); 41 } 42 for (auto iter = hashmap2.begin(); iter != hashmap2.end(); iter++) { 43 set<string> map; 44 for (auto list : iter->second) { 45 for (int i = 1;i<list.size();i++) { 46 string val = list[i]; 47 auto itr = map.find(val); 48 if (itr == map.end()) map.insert(val); 49 } 50 } 51 if (map.empty()) continue; 52 vector<string> temp(map.begin(), map.end()); 53 sort(temp.begin(), temp.end()); 54 temp.insert(temp.begin(), accounts[iter->first][0]); 55 res.push_back(temp); 56 } 57 return res; 58 } 59 };