Skip to content

Commit b84fd10

Browse files
authored
feat: add solutions to lc problem: No.2581 (doocs#2378)
No.2581.Count Number of Possible Root Nodes
1 parent 1a87080 commit b84fd10

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

solution/2500-2599/2581.Count Number of Possible Root Nodes/README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Solution {
194194
public:
195195
int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k) {
196196
int n = edges.size() + 1;
197-
vector<vector<int>> g(n);
197+
vector<int> g[n];
198198
unordered_map<long long, int> gs;
199199
auto f = [&](int i, int j) {
200200
return 1LL * i * n + j;
@@ -291,6 +291,50 @@ func rootCount(edges [][]int, guesses [][]int, k int) (ans int) {
291291
}
292292
```
293293

294+
```ts
295+
function rootCount(edges: number[][], guesses: number[][], k: number): number {
296+
const n = edges.length + 1;
297+
const g: number[][] = Array.from({ length: n }, () => []);
298+
const gs: Map<number, number> = new Map();
299+
const f = (i: number, j: number) => i * n + j;
300+
for (const [a, b] of edges) {
301+
g[a].push(b);
302+
g[b].push(a);
303+
}
304+
for (const [a, b] of guesses) {
305+
const x = f(a, b);
306+
gs.set(x, gs.has(x) ? gs.get(x)! + 1 : 1);
307+
}
308+
let ans = 0;
309+
let cnt = 0;
310+
const dfs1 = (i: number, fa: number): void => {
311+
for (const j of g[i]) {
312+
if (j !== fa) {
313+
cnt += gs.get(f(i, j)) || 0;
314+
dfs1(j, i);
315+
}
316+
}
317+
};
318+
const dfs2 = (i: number, fa: number): void => {
319+
ans += cnt >= k ? 1 : 0;
320+
for (const j of g[i]) {
321+
if (j !== fa) {
322+
const a = gs.get(f(i, j)) || 0;
323+
const b = gs.get(f(j, i)) || 0;
324+
cnt -= a;
325+
cnt += b;
326+
dfs2(j, i);
327+
cnt -= b;
328+
cnt += a;
329+
}
330+
}
331+
};
332+
dfs1(0, -1);
333+
dfs2(0, -1);
334+
return ans;
335+
}
336+
```
337+
294338
<!-- tabs:end -->
295339

296340
<!-- end -->

solution/2500-2599/2581.Count Number of Possible Root Nodes/README_EN.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class Solution {
188188
public:
189189
int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k) {
190190
int n = edges.size() + 1;
191-
vector<vector<int>> g(n);
191+
vector<int> g[n];
192192
unordered_map<long long, int> gs;
193193
auto f = [&](int i, int j) {
194194
return 1LL * i * n + j;
@@ -285,6 +285,50 @@ func rootCount(edges [][]int, guesses [][]int, k int) (ans int) {
285285
}
286286
```
287287

288+
```ts
289+
function rootCount(edges: number[][], guesses: number[][], k: number): number {
290+
const n = edges.length + 1;
291+
const g: number[][] = Array.from({ length: n }, () => []);
292+
const gs: Map<number, number> = new Map();
293+
const f = (i: number, j: number) => i * n + j;
294+
for (const [a, b] of edges) {
295+
g[a].push(b);
296+
g[b].push(a);
297+
}
298+
for (const [a, b] of guesses) {
299+
const x = f(a, b);
300+
gs.set(x, gs.has(x) ? gs.get(x)! + 1 : 1);
301+
}
302+
let ans = 0;
303+
let cnt = 0;
304+
const dfs1 = (i: number, fa: number): void => {
305+
for (const j of g[i]) {
306+
if (j !== fa) {
307+
cnt += gs.get(f(i, j)) || 0;
308+
dfs1(j, i);
309+
}
310+
}
311+
};
312+
const dfs2 = (i: number, fa: number): void => {
313+
ans += cnt >= k ? 1 : 0;
314+
for (const j of g[i]) {
315+
if (j !== fa) {
316+
const a = gs.get(f(i, j)) || 0;
317+
const b = gs.get(f(j, i)) || 0;
318+
cnt -= a;
319+
cnt += b;
320+
dfs2(j, i);
321+
cnt -= b;
322+
cnt += a;
323+
}
324+
}
325+
};
326+
dfs1(0, -1);
327+
dfs2(0, -1);
328+
return ans;
329+
}
330+
```
331+
288332
<!-- tabs:end -->
289333

290334
<!-- end -->

solution/2500-2599/2581.Count Number of Possible Root Nodes/Solution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution {
22
public:
33
int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k) {
44
int n = edges.size() + 1;
5-
vector<vector<int>> g(n);
5+
vector<int> g[n];
66
unordered_map<long long, int> gs;
77
auto f = [&](int i, int j) {
88
return 1LL * i * n + j;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function rootCount(edges: number[][], guesses: number[][], k: number): number {
2+
const n = edges.length + 1;
3+
const g: number[][] = Array.from({ length: n }, () => []);
4+
const gs: Map<number, number> = new Map();
5+
const f = (i: number, j: number) => i * n + j;
6+
for (const [a, b] of edges) {
7+
g[a].push(b);
8+
g[b].push(a);
9+
}
10+
for (const [a, b] of guesses) {
11+
const x = f(a, b);
12+
gs.set(x, gs.has(x) ? gs.get(x)! + 1 : 1);
13+
}
14+
let ans = 0;
15+
let cnt = 0;
16+
const dfs1 = (i: number, fa: number): void => {
17+
for (const j of g[i]) {
18+
if (j !== fa) {
19+
cnt += gs.get(f(i, j)) || 0;
20+
dfs1(j, i);
21+
}
22+
}
23+
};
24+
const dfs2 = (i: number, fa: number): void => {
25+
ans += cnt >= k ? 1 : 0;
26+
for (const j of g[i]) {
27+
if (j !== fa) {
28+
const a = gs.get(f(i, j)) || 0;
29+
const b = gs.get(f(j, i)) || 0;
30+
cnt -= a;
31+
cnt += b;
32+
dfs2(j, i);
33+
cnt -= b;
34+
cnt += a;
35+
}
36+
}
37+
};
38+
dfs1(0, -1);
39+
dfs2(0, -1);
40+
return ans;
41+
}

0 commit comments

Comments
 (0)