|
68 | 68 |
|
69 | 69 | <!-- 这里可写通用的实现逻辑 -->
|
70 | 70 |
|
| 71 | +**方法一:图的最大环 + 最长链** |
| 72 | + |
| 73 | +问题等价于求有向图的最大环,以及所有长度为 $2$ 的环加上其最长链。求这两者的较大值。 |
| 74 | + |
| 75 | +求最长链到长度为 $2$ 的环,可以用拓扑排序。 |
| 76 | + |
| 77 | +时间复杂度 $O(n)$。 |
| 78 | + |
| 79 | +类似题目:[2360. 图中的最长环](/solution/2300-2399/2360.Longest%20Cycle%20in%20a%20Graph/README.md) |
| 80 | + |
71 | 81 | <!-- tabs:start -->
|
72 | 82 |
|
73 | 83 | ### **Python3**
|
74 | 84 |
|
75 | 85 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
76 | 86 |
|
77 | 87 | ```python
|
78 |
| - |
| 88 | +class Solution: |
| 89 | + def maximumInvitations(self, favorite: List[int]) -> int: |
| 90 | + def max_cycle(fa): |
| 91 | + n = len(fa) |
| 92 | + vis = [False] * n |
| 93 | + ans = 0 |
| 94 | + for i in range(n): |
| 95 | + if vis[i]: |
| 96 | + continue |
| 97 | + cycle = [] |
| 98 | + j = i |
| 99 | + while not vis[j]: |
| 100 | + cycle.append(j) |
| 101 | + vis[j] = True |
| 102 | + j = fa[j] |
| 103 | + for k, v in enumerate(cycle): |
| 104 | + if v == j: |
| 105 | + ans = max(ans, len(cycle) - k) |
| 106 | + break |
| 107 | + return ans |
| 108 | + |
| 109 | + def topological_sort(fa): |
| 110 | + n = len(fa) |
| 111 | + indeg = [0] * n |
| 112 | + dist = [1] * n |
| 113 | + for v in fa: |
| 114 | + indeg[v] += 1 |
| 115 | + q = deque([i for i, v in enumerate(indeg) if v == 0]) |
| 116 | + while q: |
| 117 | + i = q.popleft() |
| 118 | + dist[fa[i]] = max(dist[fa[i]], dist[i] + 1) |
| 119 | + indeg[fa[i]] -= 1 |
| 120 | + if indeg[fa[i]] == 0: |
| 121 | + q.append(fa[i]) |
| 122 | + return sum(dist[i] for i, v in enumerate(fa) if i == fa[fa[i]]) |
| 123 | + |
| 124 | + return max(max_cycle(favorite), topological_sort(favorite)) |
79 | 125 | ```
|
80 | 126 |
|
81 | 127 | ### **Java**
|
82 | 128 |
|
83 | 129 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
84 | 130 |
|
85 | 131 | ```java
|
| 132 | +class Solution { |
| 133 | + public int maximumInvitations(int[] favorite) { |
| 134 | + return Math.max(maxCycle(favorite), topologicalSort(favorite)); |
| 135 | + } |
| 136 | + |
| 137 | + private int maxCycle(int[] fa) { |
| 138 | + int n = fa.length; |
| 139 | + boolean[] vis = new boolean[n]; |
| 140 | + int ans = 0; |
| 141 | + for (int i = 0; i < n; ++i) { |
| 142 | + if (vis[i]) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + List<Integer> cycle = new ArrayList<>(); |
| 146 | + int j = i; |
| 147 | + while (!vis[j]) { |
| 148 | + cycle.add(j); |
| 149 | + vis[j] = true; |
| 150 | + j = fa[j]; |
| 151 | + } |
| 152 | + for (int k = 0; k < cycle.size(); ++k) { |
| 153 | + if (cycle.get(k) == j) { |
| 154 | + ans = Math.max(ans, cycle.size() - k); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + return ans; |
| 159 | + } |
| 160 | + |
| 161 | + private int topologicalSort(int[] fa) { |
| 162 | + int n = fa.length; |
| 163 | + int[] indeg = new int[n]; |
| 164 | + int[] dist = new int[n]; |
| 165 | + Arrays.fill(dist, 1); |
| 166 | + for(int v : fa) { |
| 167 | + indeg[v]++; |
| 168 | + } |
| 169 | + Deque<Integer> q = new ArrayDeque<>(); |
| 170 | + for (int i = 0; i < n; ++i) { |
| 171 | + if (indeg[i] == 0) { |
| 172 | + q.offer(i); |
| 173 | + } |
| 174 | + } |
| 175 | + int ans = 0; |
| 176 | + while (!q.isEmpty()) { |
| 177 | + int i = q.pollFirst(); |
| 178 | + dist[fa[i]] = Math.max(dist[fa[i]], dist[i] + 1); |
| 179 | + if (--indeg[fa[i]] == 0) { |
| 180 | + q.offer(fa[i]); |
| 181 | + } |
| 182 | + } |
| 183 | + for (int i = 0; i < n; ++i) { |
| 184 | + if (i == fa[fa[i]]) { |
| 185 | + ans += dist[i]; |
| 186 | + } |
| 187 | + } |
| 188 | + return ans; |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +### **C++** |
| 194 | + |
| 195 | +```cpp |
| 196 | +class Solution { |
| 197 | +public: |
| 198 | + int maximumInvitations(vector<int>& favorite) { |
| 199 | + return max(maxCycle(favorite), topologicalSort(favorite)); |
| 200 | + } |
| 201 | + |
| 202 | + int maxCycle(vector<int>& fa) { |
| 203 | + int n = fa.size(); |
| 204 | + vector<bool> vis(n); |
| 205 | + int ans = 0; |
| 206 | + for (int i = 0; i < n; ++i) |
| 207 | + { |
| 208 | + if (vis[i]) continue; |
| 209 | + vector<int> cycle; |
| 210 | + int j = i; |
| 211 | + while (!vis[j]) |
| 212 | + { |
| 213 | + cycle.push_back(j); |
| 214 | + vis[j] = true; |
| 215 | + j = fa[j]; |
| 216 | + } |
| 217 | + for (int k = 0; k < cycle.size(); ++k) |
| 218 | + { |
| 219 | + if (cycle[k] == j) |
| 220 | + { |
| 221 | + ans = max(ans, (int) cycle.size() - k); |
| 222 | + break; |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + return ans; |
| 227 | + } |
| 228 | + |
| 229 | + int topologicalSort(vector<int>& fa) { |
| 230 | + int n = fa.size(); |
| 231 | + vector<int> indeg(n); |
| 232 | + vector<int> dist(n, 1); |
| 233 | + for (int v : fa) ++indeg[v]; |
| 234 | + queue<int> q; |
| 235 | + for (int i = 0; i < n; ++i) if (indeg[i] == 0) q.push(i); |
| 236 | + while (!q.empty()) |
| 237 | + { |
| 238 | + int i = q.front(); |
| 239 | + q.pop(); |
| 240 | + dist[fa[i]] = max(dist[fa[i]], dist[i] + 1); |
| 241 | + if (--indeg[fa[i]] == 0) q.push(fa[i]); |
| 242 | + } |
| 243 | + int ans = 0; |
| 244 | + for (int i = 0; i < n; ++i) if (i == fa[fa[i]]) ans += dist[i]; |
| 245 | + return ans; |
| 246 | + } |
| 247 | +}; |
| 248 | +``` |
86 | 249 |
|
| 250 | +### **Go** |
| 251 | + |
| 252 | +```go |
| 253 | +func maximumInvitations(favorite []int) int { |
| 254 | + a, b := maxCycle(favorite), topologicalSort(favorite) |
| 255 | + return max(a, b) |
| 256 | +} |
| 257 | + |
| 258 | +func maxCycle(fa []int) int { |
| 259 | + n := len(fa) |
| 260 | + vis := make([]bool, n) |
| 261 | + ans := 0 |
| 262 | + for i := range fa { |
| 263 | + if vis[i] { |
| 264 | + continue |
| 265 | + } |
| 266 | + j := i |
| 267 | + cycle := []int{} |
| 268 | + for !vis[j] { |
| 269 | + cycle = append(cycle, j) |
| 270 | + vis[j] = true |
| 271 | + j = fa[j] |
| 272 | + } |
| 273 | + for k, v := range cycle { |
| 274 | + if v == j { |
| 275 | + ans = max(ans, len(cycle)-k) |
| 276 | + break |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + return ans |
| 281 | +} |
| 282 | + |
| 283 | +func topologicalSort(fa []int) int { |
| 284 | + n := len(fa) |
| 285 | + indeg := make([]int, n) |
| 286 | + dist := make([]int, n) |
| 287 | + for i := range fa { |
| 288 | + dist[i] = 1 |
| 289 | + } |
| 290 | + for _, v := range fa { |
| 291 | + indeg[v]++ |
| 292 | + } |
| 293 | + q := []int{} |
| 294 | + for i, v := range indeg { |
| 295 | + if v == 0 { |
| 296 | + q = append(q, i) |
| 297 | + } |
| 298 | + } |
| 299 | + for len(q) > 0 { |
| 300 | + i := q[0] |
| 301 | + q = q[1:] |
| 302 | + dist[fa[i]] = max(dist[fa[i]], dist[i]+1) |
| 303 | + indeg[fa[i]]-- |
| 304 | + if indeg[fa[i]] == 0 { |
| 305 | + q = append(q, fa[i]) |
| 306 | + } |
| 307 | + } |
| 308 | + ans := 0 |
| 309 | + for i := range fa { |
| 310 | + if i == fa[fa[i]] { |
| 311 | + ans += dist[i] |
| 312 | + } |
| 313 | + } |
| 314 | + return ans |
| 315 | +} |
| 316 | + |
| 317 | +func max(a, b int) int { |
| 318 | + if a > b { |
| 319 | + return a |
| 320 | + } |
| 321 | + return b |
| 322 | +} |
87 | 323 | ```
|
88 | 324 |
|
89 | 325 | ### **TypeScript**
|
|
0 commit comments