-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount merge
144 lines (113 loc) · 3.81 KB
/
Account merge
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//Account merge
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
class GFG {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0) {
int n = sc.nextInt();
List<List<String>> accounts=new ArrayList<>();
for(int i = 0; i < n; i++) {
ArrayList<String> temp=new ArrayList<>();
int x=sc.nextInt();
for(int j = 0; j < x; j++) {
String s1=sc.next();
temp.add(s1);
}
accounts.add(temp);
}
Solution obj = new Solution();
List<List<String>> res = obj.accountsMerge(accounts);
Collections.sort(res, new Comparator<List<String>>() {
@Override public int compare(List<String> a,
List<String> b) {
int al = a.size();
int bl = b.size();
int min = Math.min(al, bl);
for(int i = 0; i < min; i++) {
String xx=a.get(i);
String yy=b.get(i);
if(xx.compareTo(yy)<0)
return -1;
else if(xx.compareTo(yy)>0)
return 1;
}
if(al < bl)
return -1;
else if(al > bl)
return 1;
return -1;
}
});
System.out.print("[");
for(int i = 0; i < res.size(); ++i) {
System.out.print("[");
for(int j = 0; j < res.get(i).size(); j++) {
if(j != res.get(i).size() - 1)
System.out.print(res.get(i).get(j)+", ");
else
System.out.print(res.get(i).get(j));
}
if(i != res.size() - 1)
System.out.println("], ");
else
System.out.print("]");
}
System.out.println("]");
}
}
}
class Solution {
static int[] par;
static List<List<String>> accountsMerge(List<List<String>> accounts) {
int n = accounts.size();
par = new int[n];
for(int i = 0; i < n; i++) {
par[i] = i;
}
Map<String,Integer> m1 = new HashMap<>();
for(int i = 0; i < n; i++) {
for (int j = 1; j < accounts.get(i).size(); j++) {
String mail = accounts.get(i).get(j);
if(m1.containsKey(mail)) {
int person = m1.get(mail);
union(i, person);
}
else {
m1.put(mail, i);
}
}
}
Map<Integer, TreeSet<String>> map = new HashMap<>();
for(int i = 0; i < n; i++) {
int root = find(i);
List<String> al = accounts.get(i);
if(!map.containsKey(root)) //logn
map.put(root, new TreeSet<String>());
map.get(root).addAll(al.subList(1, al.size()));
}
List<List<String>> res = new ArrayList<List<String>>();
for(int root : map.keySet()) {
String name = accounts.get(root).get(0);
ArrayList<String> curr = new ArrayList<>(map.get(root));
curr.add(0,name);
res.add(curr);
}
return res;
}
static int find(int idx) {
if(idx == par[idx])
return idx;
return par[idx] = find(par[idx]);
}
static void union(int x, int y){
int x_rep = find(x), y_rep=find(y);
if(x_rep==y_rep) {
return;
}
par[y_rep] = x_rep;
}
}