forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-number-of-possible-root-nodes.cpp
126 lines (120 loc) · 3.82 KB
/
count-number-of-possible-root-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
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
// Time: O(n)
// Space: O(h)
// iterative dfs
class Solution {
public:
int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k) {
unordered_map<int, vector<int>> adj;
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
unordered_set<pair<int, int>, PairHash<int>> lookup;
for (const auto& g : guesses) {
lookup.emplace(pair(g[0], g[1]));
}
const auto& iter_dfs = [&]() {
int result = 0;
vector<pair<int, int>> stk = {{0, -1}};
while (!empty(stk)) {
const auto [u, p] = stk.back(); stk.pop_back();
result += lookup.count(pair(p, u));
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
stk.emplace_back(v, u);
}
}
return result;
};
const auto& iter_dfs2 = [&](int curr) {
int result = 0;
vector<tuple<int, int, int>> stk = {{0, -1, curr}};
while (!empty(stk)) {
auto [u, p, curr] = stk.back(); stk.pop_back();
if (lookup.count(pair(p, u))) {
--curr;
}
if (lookup.count(pair(u, p))) {
++curr;
}
result += static_cast<int>(curr >= k);
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
stk.emplace_back(v, u, curr);
}
}
return result;
};
int curr = iter_dfs();
return iter_dfs2(curr);
}
private:
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
};
// Time: O(n)
// Space: O(h)
// dfs
class Solution2 {
public:
int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k) {
unordered_map<int, vector<int>> adj;
for (const auto& e : edges) {
adj[e[0]].emplace_back(e[1]);
adj[e[1]].emplace_back(e[0]);
}
unordered_set<pair<int, int>, PairHash<int>> lookup;
for (const auto& g : guesses) {
lookup.emplace(pair(g[0], g[1]));
}
const function<int(int, int)> dfs = [&](int u, int p) {
int cnt = lookup.count(pair(p, u));
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
cnt += dfs(v, u);
}
return cnt;
};
const function<int(int, int, int)> dfs2 = [&](int u, int p, int curr) {
if (lookup.count(pair(p, u))) {
--curr;
}
if (lookup.count(pair(u, p))) {
++curr;
}
int cnt = static_cast<int>(curr >= k);
for (const auto& v : adj[u]) {
if (v == p) {
continue;
}
cnt += dfs2(v, u, curr);
}
return cnt;
};
int curr = dfs(0, -1);
return dfs2(0, -1, curr);
}
private:
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
};