Aswineeey's blog

By Aswineeey, history, 4 weeks ago, In English
def IL():
    return [int(i) for i in input().split()]

def I():
    return input()

n, c = IL()
from collections import defaultdict

G = defaultdict(list)
deg = [0] * n

for _ in range(c):
    a, b = IL()
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    deg[a] += 1
    deg[b] += 1

exp = IL()
isolated_sum = sum(exp[i] for i in range(n) if deg[i] == 0)
nodes = [i for i in range(n)]

def dfs(node, taken):
    if node == n:
        return 0
    if deg[nodes[node]] == 0:
        return dfs(node + 1, taken)
    res = dfs(node + 1, taken)
    if all(not taken[nei] for nei in G[nodes[node]]):
        taken[nodes[node]] = True
        res = max(res, exp[nodes[node]] + dfs(node + 1, taken))
        taken[nodes[node]] = False
    return res

result = isolated_sum + dfs(0, [False] * n)
print(result, end="")

Problem Statement: Two developers who hate each other cannot be on the same team. The graph represents the connections between the developers, where if developer X hates developer Y, then Y also hates X and we have to form a team with maximum experience given in exp array. This is a bi-directional relationship.

  • Vote: I like it
  • +5
  • Vote: I do not like it