-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-the-number-of-good-nodes.cpp
93 lines (89 loc) · 3.02 KB
/
count-the-number-of-good-nodes.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
89
90
91
92
93
// Time: O(n)
// Space: O(h)
// iterative dfs
class Solution {
public:
int countGoodNodes(vector<vector<int>>& edges) {
vector<vector<int>> adj(size(edges) + 1);
int result = 0;
const auto& iter_dfs = [&]() {
int64_t result = 0;
using RET = int;
RET ret{};
vector<tuple<int, int, int, int, shared_ptr<RET>, shared_ptr<int>, shared_ptr<bool>, RET *>> stk = {{1, 0, -1, -1, nullptr, nullptr, nullptr, &ret}};
while (!empty(stk)) {
const auto [step, u, p, i, new_ret, l, valid, ret] = stk.back(); stk.pop_back();
if (step == 1) {
const auto& new_l = make_shared<int>();
const auto& new_valid = make_shared<bool>(true);
stk.emplace_back(4, -1, -1, -1, nullptr, nullptr, new_valid, ret);
stk.emplace_back(2, u, p, 0, nullptr, new_l, new_valid, ret);
} else if (step == 2) {
if (i == size(adj[u])) {
continue;
}
stk.emplace_back(2, u, p, i + 1, nullptr, l, valid, ret);
const auto& v = adj[u][i];
if (v == p) {
continue;
}
const auto& new_ret = make_shared<RET>();
stk.emplace_back(3, -1, -1, -1, new_ret, l, valid, ret);
stk.emplace_back(1, v, u, -1, nullptr, nullptr, nullptr, new_ret.get());
} else if (step == 3) {
(*ret) += (*new_ret);
++(*l);
if ((*new_ret) * (*l) != (*ret)) {
(*valid) = false;
}
} else if (step == 4) {
if (*valid) {
++result;
}
++(*ret);
}
}
return result;
};
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
return iter_dfs();
}
};
// Time: O(n)
// Space: O(h)
// dfs
class Solution2 {
public:
int countGoodNodes(vector<vector<int>>& edges) {
vector<vector<int>> adj(size(edges) + 1);
int result = 0;
const function<int (int, int)> dfs = [&](int u, int p) {
int total = 0, l = 0;
bool valid = true;
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
const int cnt = dfs(v, u);
total += cnt;
++l;
if (cnt * l != total) {
valid = false;
}
}
if (valid) {
++result;
}
return total + 1;
};
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
dfs(0, -1);
return result;
}
};