-2
튜플 목록이 있습니다. (1,2,3), (4,5), (4,7), (7,9), (3,5), (5,6), (11,12,13,14) (1,2)가 도메인에 속하고 (2,3)를 [1,2,3] 동일한 도메인에 속하는 도메인에 속하는 경우 17)]튜플 목록에서 값을 그룹화하는 방법은 무엇입니까?
관계는
이다.
출력되어야
{ 'C1'{1, 2, 3, 5, 4, 7, 9, 6}, "C11": {11, 17}}
이런 내가 피곤하지만 큰/복잡한 입력 실패 (예를 들어 내가 = 500 때)
튜플 당신이 disjoint set을 구현하기 위해 노력하고있는 것 같습니다
N, I = input().strip().split()
N, I = [int(N), int(I)]
connections = {}
for i in range(I):
l, m = input().strip().split()
l, m = [int(l), int(m)]
if(connections == {}):
connections['c'+str(l)] = set([l, m])
for key in list(connections):
if l in connections[key] or m in connections[key]:
connections[key].update([l, m])
break
else:
connections['c'+str(l)] = set([l, m])
print(connections)
기본 그래프 이론 : 그래프의 연결된 구성 요소를 찾는 것. 또한 귀하의 질문은 무엇입니까? – Julien