-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
88 lines (78 loc) · 2.77 KB
/
main.cpp
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
class Solution {
public:
vector<double> calcEquation(vector<vector<string>>& equations,
vector<double>& values,
vector<vector<string>>& queries) {
unordered_map<string, unordered_map<string, double>> graph;
for (int i = 0; i < equations.size(); ++i) {
string a = equations[i][0];
string b = equations[i][1];
double v = values[i];
graph[a][b] = v;
graph[b][a] = 1 / v;
}
unordered_set<string> vis;
function<void(string)> dfs = [&](string u) -> void {
if (!graph.count(u)) return;
if (vis.count(u)) return;
vis.insert(u);
for (auto [v, v_val] : graph[u]) {
dfs(v);
for (auto [w, w_val] : graph[v]) {
graph[u][w] = v_val * w_val;
graph[w][u] = 1 / graph[u][w];
}
}
};
vector<double> result(queries.size(), -1);
for (int i = 0; i < queries.size(); ++i) {
string a = queries[i][0], b = queries[i][1];
dfs(a);
if (auto it = graph[a].find(b); it != graph[a].end())
result[i] = it->second;
}
return result;
}
};
class Solution {
public:
vector<double> calcEquation(vector<vector<string>>& equations,
vector<double>& values,
vector<vector<string>>& queries) {
unordered_map<string, unordered_map<string, double>> mp;
for (int i = 0; i < equations.size(); ++i) {
auto a = equations[i][0];
auto b = equations[i][1];
auto v = values[i];
mp[a][b] = v;
if (v != 0) mp[b][a] = 1.0 / v;
}
unordered_set<string> vis;
function<double(string, string)> dfs = [&](string a,
string t) -> double {
if (a == t) return 1.0;
if (mp[a].count(t) > 0 && mp[a][t] >= -1.0) return mp[a][t];
if (vis.count(a) > 0) return -1.0;
vis.insert(a);
for (auto [b, v] : mp[a]) {
if (v < 0) continue;
auto got = dfs(b, t);
if (got < 0) continue;
return mp[a][t] = v * got;
}
return -1.0;
};
vector<double> result;
for (auto& query : queries) {
vis.clear();
auto a = query[0];
auto b = query[1];
if (mp.count(a) + mp.count(b) != 2) {
result.push_back(-1.0);
} else {
result.push_back(dfs(query[0], query[1]));
}
}
return result;
}
};