-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0399_calcEquation.cc
253 lines (236 loc) · 5.7 KB
/
Problem_0399_calcEquation.cc
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <queue>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
class Solution
{
public:
// bfs
vector<double> calcEquation1(vector<vector<string>>& equations,
vector<double>& values,
vector<vector<string>>& queries)
{
int vars = 0;
// 统计节点
unordered_map<string, int> variables;
int n = equations.size();
for (int i = 0; i < n; i++)
{
if (!variables.count(equations[i][0]))
{
variables[equations[i][0]] = vars++;
}
if (!variables.count(equations[i][1]))
{
variables[equations[i][1]] = vars++;
}
}
// 建图
vector<vector<std::pair<int, double>>> edges(vars);
for (int i = 0; i < n; i++)
{
int idxA = variables[equations[i][0]];
int idxB = variables[equations[i][1]];
edges[idxA].push_back({idxB, values[i]});
edges[idxB].push_back({idxA, 1.0 / values[i]});
}
vector<double> ans;
for (auto& q : queries)
{
double res = -1.0;
if (variables.count(q[0]) && variables.count(q[1]))
{
int idxA = variables[q[0]];
int idxB = variables[q[1]];
if (idxA == idxB)
{
res = 1.0;
}
else
{
queue<int> points;
points.push(idxA);
vector<double> ratios(vars, -1.0);
ratios[idxA] = 1.0;
while (!points.empty() && ratios[idxB] < 0)
{
int cur = points.front();
points.pop();
for (auto& [next, val] : edges[cur])
{
if (ratios[next] < 0)
{
// 没计算过的才需要计算
// ratios数组相当于一般 bfs 的 vsisted 数组
ratios[next] = ratios[cur] * val;
points.push(next);
}
}
}
res = ratios[idxB];
}
}
ans.push_back(res);
}
return ans;
}
// 对于查询数量很多的情形,如果为每次查询都独立搜索一次,则效率会变低。
// 为此,我们不妨对图先做一定的预处理,随后就可以在较短的时间内回答每个查询。
// 利用 Floyd 算法,先计算出任意两点之间的距离
vector<double> calcEquation2(vector<vector<string>>& equations,
vector<double>& values,
vector<vector<string>>& queries)
{
int vars = 0;
// 统计节点
unordered_map<string, int> variables;
int n = equations.size();
for (int i = 0; i < n; i++)
{
if (!variables.count(equations[i][0]))
{
variables[equations[i][0]] = vars++;
}
if (!variables.count(equations[i][1]))
{
variables[equations[i][1]] = vars++;
}
}
// 建图
vector<vector<double>> edges(vars, vector<double>(vars, -1.0));
for (int i = 0; i < n; i++)
{
int idxA = variables[equations[i][0]];
int idxB = variables[equations[i][1]];
edges[idxA][idxB] = values[i];
edges[idxB][idxA] = 1.0 / values[i];
}
// 预处理
for (int k = 0; k < vars; k++)
{
for (int i = 0; i < vars; i++)
{
for (int j = 0; j < vars; j++)
{
if (edges[i][k] > 0 && edges[k][j] > 0)
{
edges[i][j] = edges[i][k] * edges[k][j];
}
}
}
}
vector<double> ans;
for (auto& q : queries)
{
double res = -1.0;
if (variables.count(q[0]) && variables.count(q[1]))
{
int idxA = variables[q[0]];
int idxB = variables[q[1]];
if (edges[idxA][idxB] > 0)
{
res = edges[idxA][idxB];
}
}
ans.push_back(res);
}
return ans;
}
// 并查集
vector<double> calcEquation3(vector<vector<string>>& equations,
vector<double>& values,
vector<vector<string>>& queries)
{
int n = equations.size();
UnionFind uf(2 * n);
unordered_map<string, int> map;
int id = 0;
for (int i = 0; i < n; i++)
{
string a = equations[i][0];
string b = equations[i][1];
if (!map.count(a))
{
map[a] = id;
id++;
}
if (!map.count(b))
{
map[b] = id;
id++;
}
uf.unions(map[a], map[b], values[i]);
}
int m = queries.size();
vector<double> ans(m);
for (int i = 0; i < m; i++)
{
string a = queries[i][0];
string b = queries[i][1];
if (!map.count(a) || !map.count(b))
{
ans[i] = -1.0;
}
else
{
int idA = map[a];
int idB = map[b];
ans[i] = uf.isConnected(idA, idB);
}
}
return ans;
}
class UnionFind
{
private:
vector<int> parent;
vector<double> weight;
public:
UnionFind(int n)
{
parent.resize(n);
weight.resize(n);
for (int i = 0; i < n; i++)
{
parent[i] = i;
weight[i] = 1.0;
}
}
int find(int x)
{
if (x != parent[x])
{
int c = parent[x];
parent[x] = find(parent[x]);
weight[x] *= weight[c];
}
return parent[x];
}
void unions(int x, int y, double value)
{
int px = find(x);
int py = find(y);
if (px != py)
{
parent[px] = py;
// TODO: 这里理解比较绕
weight[px] = weight[y] * value / weight[x];
}
}
double isConnected(int x, int y)
{
int px = find(x);
int py = find(y);
if (px == py)
{
return weight[x] / weight[y];
}
else
{
return -1.0;
}
}
};
};