Skip to content

Commit 9c8ef31

Browse files
authored
feat: add solutions to lc problem: No.2876 (#1742)
No.2876.Count Visited Nodes in a Directed Graph
1 parent 186da5e commit 9c8ef31

File tree

7 files changed

+420
-33
lines changed

7 files changed

+420
-33
lines changed

solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README.md

+149-3
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,81 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:基环树 + 遍历搜索**
59+
60+
我们可以用一个数组 $ans$ 记录每个节点的答案,用一个数组 $vis$ 记录每个节点的访问次序。
61+
62+
遍历每个节点 $i$,如果当前节点 $i$ 未被访问,我们就从节点 $i$ 开始遍历,此时有两种情况:
63+
64+
1. 如果遍历过程中,遇到了当前节点出发时走过的节点,那么此次遍历,一定是先走到了环内,然后沿着环走了一圈。对于环外的节点,其答案就是环的长度加上节点到环的距离;对于环内的节点,其答案就是环的长度。
65+
1. 如果遍历过程中,遇到了此前节点出发时走过的节点,那么对于每个走过的节点,其答案就是当前节点到此节点的距离,加上此节点的答案。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $edges$ 的长度。
68+
5869
<!-- tabs:start -->
5970

6071
### **Python3**
6172

6273
<!-- 这里可写当前语言的特殊实现逻辑 -->
6374

6475
```python
65-
76+
class Solution:
77+
def countVisitedNodes(self, edges: List[int]) -> List[int]:
78+
n = len(edges)
79+
ans = [0] * n
80+
vis = [0] * n
81+
for i in range(n):
82+
if not ans[i]:
83+
cnt, j = 0, i
84+
while not vis[j]:
85+
cnt += 1
86+
vis[j] = cnt
87+
j = edges[j]
88+
cycle, total = 0, cnt + ans[j]
89+
if not ans[j]:
90+
cycle = cnt - vis[j] + 1
91+
total = cnt
92+
j = i
93+
while not ans[j]:
94+
ans[j] = max(total, cycle)
95+
total -= 1
96+
j = edges[j]
97+
return ans
6698
```
6799

68100
### **Java**
69101

70102
<!-- 这里可写当前语言的特殊实现逻辑 -->
71103

104+
```java
105+
class Solution {
106+
public int[] countVisitedNodes(List<Integer> edges) {
107+
int n = edges.size();
108+
int[] ans = new int[n];
109+
int[] vis = new int[n];
110+
for (int i = 0; i < n; ++i) {
111+
if (ans[i] == 0) {
112+
int cnt = 0, j = i;
113+
while (vis[j] == 0) {
114+
vis[j] = ++cnt;
115+
j = edges.get(j);
116+
}
117+
int cycle = 0, total = cnt + ans[j];
118+
if (ans[j] == 0) {
119+
cycle = cnt - vis[j] + 1;
120+
}
121+
j = i;
122+
while (ans[j] == 0) {
123+
ans[j] = Math.max(total--, cycle);
124+
j = edges.get(j);
125+
}
126+
}
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
72133
```java
73134
class Solution {
74135
void dfs(int curr, List<Integer> edges, int[] ans) {
@@ -106,19 +167,104 @@ class Solution {
106167
return ans;
107168
}
108169
}
109-
110170
```
111171

112172
### **C++**
113173

114174
```cpp
115-
175+
class Solution {
176+
public:
177+
vector<int> countVisitedNodes(vector<int>& edges) {
178+
int n = edges.size();
179+
vector<int> ans(n), vis(n);
180+
for (int i = 0; i < n; ++i) {
181+
if (!ans[i]) {
182+
int cnt = 0, j = i;
183+
while (vis[j] == 0) {
184+
vis[j] = ++cnt;
185+
j = edges[j];
186+
}
187+
int cycle = 0, total = cnt + ans[j];
188+
if (ans[j] == 0) {
189+
cycle = cnt - vis[j] + 1;
190+
}
191+
j = i;
192+
while (ans[j] == 0) {
193+
ans[j] = max(total--, cycle);
194+
j = edges[j];
195+
}
196+
}
197+
}
198+
return ans;
199+
}
200+
};
116201
```
117202
118203
### **Go**
119204
120205
```go
206+
func countVisitedNodes(edges []int) []int {
207+
n := len(edges)
208+
ans := make([]int, n)
209+
vis := make([]int, n)
210+
for i := range ans {
211+
if ans[i] == 0 {
212+
cnt, j := 0, i
213+
for vis[j] == 0 {
214+
cnt++
215+
vis[j] = cnt
216+
j = edges[j]
217+
}
218+
cycle, total := 0, cnt+ans[j]
219+
if ans[j] == 0 {
220+
cycle = cnt - vis[j] + 1
221+
}
222+
j = i
223+
for ans[j] == 0 {
224+
ans[j] = max(total, cycle)
225+
total--
226+
j = edges[j]
227+
}
228+
}
229+
}
230+
return ans
231+
}
121232
233+
func max(a, b int) int {
234+
if a > b {
235+
return a
236+
}
237+
return b
238+
}
239+
```
240+
241+
### **TypeScript**
242+
243+
```ts
244+
function countVisitedNodes(edges: number[]): number[] {
245+
const n = edges.length;
246+
const ans: number[] = Array(n).fill(0);
247+
const vis: number[] = Array(n).fill(0);
248+
for (let i = 0; i < n; ++i) {
249+
if (ans[i] === 0) {
250+
let [cnt, j] = [0, i];
251+
while (vis[j] === 0) {
252+
vis[j] = ++cnt;
253+
j = edges[j];
254+
}
255+
let [cycle, total] = [0, cnt + ans[j]];
256+
if (ans[j] === 0) {
257+
cycle = cnt - vis[j] + 1;
258+
}
259+
j = i;
260+
while (ans[j] === 0) {
261+
ans[j] = Math.max(total--, cycle);
262+
j = edges[j];
263+
}
264+
}
265+
}
266+
return ans;
267+
}
122268
```
123269

124270
### **...**

solution/2800-2899/2876.Count Visited Nodes in a Directed Graph/README_EN.md

+149-3
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,77 @@
4949

5050
## Solutions
5151

52+
**Solution 1: Basic Tree + Traversal**
53+
54+
We can use an array $ans$ to record the answer for each node, and an array $vis$ to record the visit order for each node.
55+
56+
For each node $i$, if it has not been visited yet, we start traversing from node $i$. There are two cases:
57+
58+
- If we encounter a node that has been visited before during the traversal, then we must have first entered the cycle and then walked around the cycle. For nodes outside the cycle, their answer is the length of the cycle plus the distance from the node to the cycle; for nodes inside the cycle, their answer is the length of the cycle.
59+
- If we encounter a node that has been visited before during the traversal, then for each visited node, its answer is the distance from the current node to this node plus the answer of this node.
60+
61+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array edges.
62+
5263
<!-- tabs:start -->
5364

5465
### **Python3**
5566

5667
```python
57-
68+
class Solution:
69+
def countVisitedNodes(self, edges: List[int]) -> List[int]:
70+
n = len(edges)
71+
ans = [0] * n
72+
vis = [0] * n
73+
for i in range(n):
74+
if not ans[i]:
75+
cnt, j = 0, i
76+
while not vis[j]:
77+
cnt += 1
78+
vis[j] = cnt
79+
j = edges[j]
80+
cycle, total = 0, cnt + ans[j]
81+
if not ans[j]:
82+
cycle = cnt - vis[j] + 1
83+
total = cnt
84+
j = i
85+
while not ans[j]:
86+
ans[j] = max(total, cycle)
87+
total -= 1
88+
j = edges[j]
89+
return ans
5890
```
5991

6092
### **Java**
6193

94+
```java
95+
class Solution {
96+
public int[] countVisitedNodes(List<Integer> edges) {
97+
int n = edges.size();
98+
int[] ans = new int[n];
99+
int[] vis = new int[n];
100+
for (int i = 0; i < n; ++i) {
101+
if (ans[i] == 0) {
102+
int cnt = 0, j = i;
103+
while (vis[j] == 0) {
104+
vis[j] = ++cnt;
105+
j = edges.get(j);
106+
}
107+
int cycle = 0, total = cnt + ans[j];
108+
if (ans[j] == 0) {
109+
cycle = cnt - vis[j] + 1;
110+
}
111+
j = i;
112+
while (ans[j] == 0) {
113+
ans[j] = Math.max(total--, cycle);
114+
j = edges.get(j);
115+
}
116+
}
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
62123
```java
63124
class Solution {
64125
void dfs(int curr, List<Integer> edges, int[] ans) {
@@ -96,19 +157,104 @@ class Solution {
96157
return ans;
97158
}
98159
}
99-
100160
```
101161

102162
### **C++**
103163

104164
```cpp
105-
165+
class Solution {
166+
public:
167+
vector<int> countVisitedNodes(vector<int>& edges) {
168+
int n = edges.size();
169+
vector<int> ans(n), vis(n);
170+
for (int i = 0; i < n; ++i) {
171+
if (!ans[i]) {
172+
int cnt = 0, j = i;
173+
while (vis[j] == 0) {
174+
vis[j] = ++cnt;
175+
j = edges[j];
176+
}
177+
int cycle = 0, total = cnt + ans[j];
178+
if (ans[j] == 0) {
179+
cycle = cnt - vis[j] + 1;
180+
}
181+
j = i;
182+
while (ans[j] == 0) {
183+
ans[j] = max(total--, cycle);
184+
j = edges[j];
185+
}
186+
}
187+
}
188+
return ans;
189+
}
190+
};
106191
```
107192
108193
### **Go**
109194
110195
```go
196+
func countVisitedNodes(edges []int) []int {
197+
n := len(edges)
198+
ans := make([]int, n)
199+
vis := make([]int, n)
200+
for i := range ans {
201+
if ans[i] == 0 {
202+
cnt, j := 0, i
203+
for vis[j] == 0 {
204+
cnt++
205+
vis[j] = cnt
206+
j = edges[j]
207+
}
208+
cycle, total := 0, cnt+ans[j]
209+
if ans[j] == 0 {
210+
cycle = cnt - vis[j] + 1
211+
}
212+
j = i
213+
for ans[j] == 0 {
214+
ans[j] = max(total, cycle)
215+
total--
216+
j = edges[j]
217+
}
218+
}
219+
}
220+
return ans
221+
}
222+
223+
func max(a, b int) int {
224+
if a > b {
225+
return a
226+
}
227+
return b
228+
}
229+
```
230+
231+
### **TypeScript**
111232

233+
```ts
234+
function countVisitedNodes(edges: number[]): number[] {
235+
const n = edges.length;
236+
const ans: number[] = Array(n).fill(0);
237+
const vis: number[] = Array(n).fill(0);
238+
for (let i = 0; i < n; ++i) {
239+
if (ans[i] === 0) {
240+
let [cnt, j] = [0, i];
241+
while (vis[j] === 0) {
242+
vis[j] = ++cnt;
243+
j = edges[j];
244+
}
245+
let [cycle, total] = [0, cnt + ans[j]];
246+
if (ans[j] === 0) {
247+
cycle = cnt - vis[j] + 1;
248+
}
249+
j = i;
250+
while (ans[j] === 0) {
251+
ans[j] = Math.max(total--, cycle);
252+
j = edges[j];
253+
}
254+
}
255+
}
256+
return ans;
257+
}
112258
```
113259

114260
### **...**

0 commit comments

Comments
 (0)