@@ -131,42 +131,27 @@ d[find(a)] = distance
131
131
``` python
132
132
class Solution :
133
133
def calcEquation (self , equations : List[List[str ]], values : List[float ], queries : List[List[str ]]) -> List[float ]:
134
- n = len (equations)
135
- p = list (range (n << 1 ))
136
- w = [1.0 ] * (n << 1 )
137
-
134
+ w = collections.defaultdict(lambda : 1 )
135
+ p = collections.defaultdict()
136
+ for a, b in equations:
137
+ p[a] = a
138
+ p[b] = b
139
+
138
140
def find (x ):
139
141
if p[x] != x:
140
142
origin = p[x]
141
143
p[x] = find(p[x])
142
144
w[x] *= w[origin]
143
145
return p[x]
144
146
145
- mp = {}
146
- idx = 0
147
147
for i, e in enumerate (equations):
148
- a, b = e[0 ], e[1 ]
149
- if a not in mp:
150
- mp[a] = idx
151
- idx += 1
152
- if b not in mp:
153
- mp[b] = idx
154
- idx += 1
155
- pa, pb = find(mp[a]), find(mp[b])
148
+ pa, pb = find(e[0 ]), find(e[1 ])
156
149
if pa == pb:
157
150
continue
158
151
p[pa] = pb
159
- w[pa] = w[mp[b]] * values[i] / w[mp[a]]
160
-
161
- res = []
162
- for q in queries:
163
- c, d = q[0 ], q[1 ]
164
- if c not in mp or d not in mp:
165
- res.append(- 1.0 )
166
- else :
167
- pa, pb = find(mp[c]), find(mp[d])
168
- res.append(w[mp[c]] / w[mp[d]] if pa == pb else - 1.0 )
169
- return res
152
+ w[pa] = w[e[1 ]] * values[i] / w[e[0 ]]
153
+
154
+ return [- 1 if c not in p or d not in p or find(c) != find(d) else w[c] / w[d] for c, d in queries]
170
155
```
171
156
172
157
### ** Java**
@@ -175,57 +160,45 @@ class Solution:
175
160
176
161
``` java
177
162
class Solution {
178
- private int [] p;
179
- private double [] w;
163
+ private Map< String , String > p;
164
+ private Map< String , Double > w;
180
165
181
166
public double [] calcEquation (List<List<String > > equations , double [] values , List<List<String > > queries ) {
182
167
int n = equations. size();
183
- p = new int [n << 1 ];
184
- w = new double [n << 1 ];
185
- for (int i = 0 ; i < p. length; ++ i) {
186
- p[i] = i;
187
- w[i] = 1.0 ;
168
+ p = new HashMap<> (n << 1 );
169
+ w = new HashMap<> (n << 1 );
170
+ for (List<String > e : equations) {
171
+ p. put(e. get(0 ), e. get(0 ));
172
+ p. put(e. get(1 ), e. get(1 ));
173
+ w. put(e. get(0 ), 1.0 );
174
+ w. put(e. get(1 ), 1.0 );
188
175
}
189
- Map<String , Integer > mp = new HashMap<> (n << 1 );
190
- int idx = 0 ;
191
176
for (int i = 0 ; i < n; ++ i) {
192
177
List<String > e = equations. get(i);
193
178
String a = e. get(0 ), b = e. get(1 );
194
- if (! mp. containsKey(a)) {
195
- mp. put(a, idx++ );
196
- }
197
- if (! mp. containsKey(b)) {
198
- mp. put(b, idx++ );
199
- }
200
- int pa = find(mp. get(a)), pb = find(mp. get(b));
201
- if (pa == pb) {
179
+ String pa = find(a), pb = find(b);
180
+ if (Objects . equals(pa, pb)) {
202
181
continue ;
203
182
}
204
- p[pa] = pb ;
205
- w[pa] = w[mp . get(b)] * values[i] / w[mp . get(a)] ;
183
+ p. put(pa, pb) ;
184
+ w. put(pa, w . get(b) * values[i] / w. get(a)) ;
206
185
}
207
186
int m = queries. size();
208
187
double [] res = new double [m];
209
188
for (int i = 0 ; i < m; ++ i) {
210
189
String c = queries. get(i). get(0 ), d = queries. get(i). get(1 );
211
- Integer id1 = mp. get(c), id2 = mp. get(d);
212
- if (id1 == null || id2 == null ) {
213
- res[i] = - 1.0 ;
214
- } else {
215
- int pa = find(id1), pb = find(id2);
216
- res[i] = pa == pb ? w[id1] / w[id2] : - 1.0 ;
217
- }
190
+ res[i] = ! p. containsKey(c) || ! p. containsKey(d) || ! Objects . equals(find(c), find(d)) ? - 1.0 : w. get(c) / w. get(d);
218
191
}
219
192
return res;
220
193
}
221
194
222
- private int find (int x ) {
223
- if (p[x] != x ) {
224
- int origin = p[x] ;
225
- p[x] = find(p[x] );
226
- w[x] *= w[ origin] ;
195
+ private String find (String x ) {
196
+ if (! Objects . equals(p . get(x), x) ) {
197
+ String origin = p. get(x) ;
198
+ p. put(x, find(p. get(x)) );
199
+ w. put(x, w . get(x) * w . get( origin)) ;
227
200
}
228
- return p[x] ;
201
+ return p. get(x) ;
229
202
}
230
203
}
231
204
```
@@ -235,48 +208,41 @@ class Solution {
235
208
``` cpp
236
209
class Solution {
237
210
public:
238
- vector< int > p;
239
- vector< double > w;
211
+ unordered_map<string, string > p;
212
+ unordered_map<string, double> w;
240
213
241
214
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
242
215
int n = equations.size();
243
- for (int i = 0; i < (n << 1); ++i )
216
+ for (auto e : equations )
244
217
{
245
- p.push_back(i);
246
- w.push_back(1.0);
218
+ p[e[0]] = e[0];
219
+ p[e[1]] = e[1];
220
+ w[e[0]] = 1.0;
221
+ w[e[1]] = 1.0;
247
222
}
248
- unordered_map<string, int > mp;
249
- int idx = 0 ;
250
223
for (int i = 0 ; i < n; ++i)
251
224
{
252
- auto e = equations[i];
225
+ vector<string> e = equations[i];
253
226
string a = e[0], b = e[1];
254
- if (mp.find(a) == mp.end()) mp[a] = idx++;
255
- if (mp.find(b) == mp.end()) mp[b] = idx++;
256
- int pa = find(mp[a]), pb = find(mp[b]);
227
+ string pa = find(a), pb = find(b);
257
228
if (pa == pb) continue;
258
229
p[pa] = pb;
259
- w[pa] = w[mp[b]] * values[i] / w[mp[a] ];
230
+ w[pa] = w[b] * values[i] / w[a ];
260
231
}
261
232
int m = queries.size();
262
- vector<double> res;
233
+ vector<double> res(m) ;
263
234
for (int i = 0; i < m; ++i)
264
235
{
265
236
string c = queries[i][0], d = queries[i][1];
266
- if (mp.find(c) == mp.end() || mp.find(d) == mp.end()) res.push_back(-1.0);
267
- else
268
- {
269
- int pa = find(mp[c]), pb = find(mp[d]);
270
- res.push_back(pa == pb ? w[mp[c]] / w[mp[d]] : -1.0);
271
- }
237
+ res[i] = p.find(c) == p.end() || p.find(d) == p.end() || find(c) != find(d) ? -1.0 : w[c] / w[d];
272
238
}
273
239
return res;
274
240
}
275
241
276
- int find(int x) {
242
+ string find(string x) {
277
243
if (p[x] != x)
278
244
{
279
- int origin = p[x];
245
+ string origin = p[x];
280
246
p[x] = find(p[x]);
281
247
w[x] *= w[origin];
282
248
}
@@ -288,54 +254,40 @@ public:
288
254
### ** Go**
289
255
290
256
``` go
291
- var p [] int
292
- var w [ ]float64
257
+ var p map [ string ] string
258
+ var w map [ string ]float64
293
259
294
260
func calcEquation (equations [][]string , values []float64 , queries [][]string ) []float64 {
295
- n := len (equations)
296
- p = make ([]int , (n<<1 )+10 )
297
- w = make ([]float64 , (n<<1 )+10 )
298
- for i := 0 ; i < (n<<1 )+10 ; i++ {
299
- p[i] = i
300
- w[i] = 1.0
261
+ p = make (map [string ]string )
262
+ w = make (map [string ]float64 )
263
+ for _ , e := range equations {
264
+ p[e[0 ]] = e[0 ]
265
+ p[e[1 ]] = e[1 ]
266
+ w[e[0 ]] = 1.0
267
+ w[e[1 ]] = 1.0
301
268
}
302
- mp := make (map [string ]int )
303
- idx := 1
304
269
for i , e := range equations {
305
270
a , b := e[0 ], e[1 ]
306
- if mp[a] == 0 {
307
- mp[a] = idx
308
- idx++
309
- }
310
- if mp[b] == 0 {
311
- mp[b] = idx
312
- idx++
313
- }
314
- pa , pb := find (mp[a]), find (mp[b])
271
+ pa , pb := find (a), find (b)
315
272
if pa == pb {
316
273
continue
317
274
}
318
275
p[pa] = pb
319
- w[pa] = w[mp[b]] * values[i] / w[mp[a] ]
276
+ w[pa] = w[b] * values[i] / w[a ]
320
277
}
321
278
var res []float64
322
- for _ , q := range queries {
323
- c , d := q [0 ], q [1 ]
324
- if mp [c] == 0 || mp [d] == 0 {
279
+ for _ , e := range queries {
280
+ c , d := e [0 ], e [1 ]
281
+ if p [c] == " " || p [d] == " " || find (c) != find (d) {
325
282
res = append (res, -1.0 )
326
283
} else {
327
- pa , pb := find (mp[c]), find (mp[d])
328
- if pa == pb {
329
- res = append (res, w[mp[c]]/w[mp[d]])
330
- } else {
331
- res = append (res, -1.0 )
332
- }
284
+ res = append (res, w[c]/w[d])
333
285
}
334
286
}
335
287
return res
336
288
}
337
289
338
- func find (x int ) int {
290
+ func find (x string ) string {
339
291
if p[x] != x {
340
292
origin := p[x]
341
293
p[x] = find (p[x])
0 commit comments