1615. 最大网络秩
直接模拟即可
class Solution:
def maximalNetworkRank(self, n: int, roads: List[List[int]]) -> int:
adjs = defaultdict(set) # 注意是集合
for u, v in roads:
adjs[u].add(v)
adjs[v].add(u)
res = 0
for i in range(n):
for j in range(i + 1, n):
cur = len(adjs[i]) + len(adjs[j]) # 注意是当前节点边数之和
if j in adjs[i]:
cur -= 1
res = max(res, cur)
return res