Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #data1 has an answer of 46 https://pastebin.com/GY523XGA
- #data has an answer of 82 https://pastebin.com/pkJsfPg3
- from collections import defaultdict
- edges = []
- graph = {}
- f = open("data1.txt", "r")
- m = int(f.readline())
- for line in f:
- edge = line.split(" ")
- edge = list(map(lambda item: int(item), edge))
- if len(edge) == 1:
- edge.append(None)
- edges.append(edge)
- # graph = dict.fromkeys([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], )
- graph = dict.fromkeys(range(1, m + 1), )
- print(edges)
- i = 1
- for edge in edges:
- left = edge[0]
- right = edge[1]
- if left is not None and right is not None:
- if graph[left] is None and graph[right] is None:
- graph[left] = i
- graph[right] = i
- i = i + 1
- elif graph[left] is None and graph[right] is not None:
- graph[left] = graph[right]
- elif graph[left] is not None and graph[right] is None:
- graph[right] = graph[left]
- else:
- anyone = edge[0]
- if graph[anyone] is None:
- graph[anyone] = i
- for edge in edges:
- left = edge[0]
- right = edge[1]
- if (left is not None and right is not None) and (graph[left] != graph[right]) and left != right:
- ind1 = graph[right]
- for (k, v) in graph.items():
- if v == ind1 :
- graph[k] = graph[left]
- for (k, v) in graph.items():
- if v is None :
- i = i+1
- graph[k] = i
- print(graph)
- res = defaultdict(list)
- for key, val in sorted(graph.items()):
- res[val].append(key)
- print(len(res) - 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement