Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] main from itcharge:main #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 2 commits into AlgorithmAndLeetCode:main from itcharge:main
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
更新题解列表
  • Loading branch information
itcharge committed Jul 12, 2023
commit a3476f4be393cf27c736ce1e3af1e823568798f3
45 changes: 19 additions & 26 deletions Solutions/1617. 统计子树中城市之间最大距离.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,34 @@

```Python
class Solution:
def __init__(self):
self.visited = 0
self.diameter = 0

def dfs(self, graph, mask, u):
self.visited |= 1 << u # 标记 u 访问过
u_len = 0 # u 节点的最大路径长度
for v in graph[u]: # 遍历 u 节点的相邻节点
if self.visited >> v & 1 == 0 and mask >> v & 1: # v 没有访问过,且在子集中
v_len = self.dfs(graph, mask, v) # 相邻节点的最大路径长度
self.diameter = max(self.diameter, u_len + v_len + 1) # 维护最大路径长度
u_len = max(u_len, v_len + 1) # 更新 u 节点的最大路径长度
return u_len



def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:
# 建图
graph = [[] for _ in range(n)]
graph = [[] for _ in range(n)] # 建图
for u, v in edges:
graph[u - 1].append(v - 1)
graph[v - 1].append(u - 1)

def dfs(mask, u):
nonlocal visited, diameter
visited |= 1 << u # 标记 u 访问过
u_len = 0 # u 节点的最大路径长度
for v in graph[u]: # 遍历 u 节点的相邻节点
if (visited >> v) & 1 == 0 and mask >> v & 1: # v 没有访问过,且在子集中
v_len = dfs(mask, v) # 相邻节点的最大路径长度
diameter = max(diameter, u_len + v_len + 1) # 维护最大路径长度
u_len = max(u_len, v_len + 1) # 更新 u 节点的最大路径长度
return u_len

ans = [0 for _ in range(n - 1)]


for mask in range(3, 1 << n): # 二进制枚举子集
if mask & (mask - 1) == 0: # 子集至少需要两个点
for mask in range(3, 1 << n): # 二进制枚举子集
if mask & (mask - 1) == 0: # 子集至少需要两个点
continue
self.visited = 0
self.diameter = 0
visited = 0
diameter = 0
u = mask.bit_length() - 1
self.dfs(graph, mask, u) # 在子集 mask 中递归求树的直径
if self.visited == mask:
ans[self.diameter - 1] += 1
dfs(mask, u) # 在子集 mask 中递归求树的直径
if visited == mask:
ans[diameter - 1] += 1
return ans
```

Expand Down
28 changes: 13 additions & 15 deletions Solutions/2246. 相邻字符不同的最长路径.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@

```Python
class Solution:
def __init__(self):
self.ans = 0

def dfs(self, graph, s, u):
u_len = 0 # u 节点的最大路径长度
for v in graph[u]: # 遍历 u 节点的相邻节点
v_len = self.dfs(graph, s, v) # 相邻节点的最大路径长度
if s[u] != s[v]: # 相邻节点字符不同
self.ans = max(self.ans, u_len + v_len + 1) # 维护最大路径长度
u_len = max(u_len, v_len + 1) # 更新 u 节点的最大路径长度
return u_len # 返回 u 节点的最大路径长度

def longestPath(self, parent: List[int], s: str) -> int:
size = len(parent)

Expand All @@ -90,9 +78,19 @@ class Solution:
for i in range(1, size):
graph[parent[i]].append(i)

self.dfs(graph, s, 0)

return self.ans + 1
ans = 0
def dfs(u):
nonlocal ans
u_len = 0 # u 节点的最大路径长度
for v in graph[u]: # 遍历 u 节点的相邻节点
v_len = dfs(v) # 相邻节点的最大路径长度
if s[u] != s[v]: # 相邻节点字符不同
ans = max(ans, u_len + v_len + 1) # 维护最大路径长度
u_len = max(u_len, v_len + 1) # 更新 u 节点的最大路径长度
return u_len # 返回 u 节点的最大路径长度

dfs(0)
return ans + 1
```

### 思路 1:复杂度分析
Expand Down

AltStyle によって変換されたページ (->オリジナル) /