Created
December 14, 2014 15:43
-
-
Save binhngoc17/959b1c8853a263c54ad5 to your computer and use it in GitHub Desktop.
Use combinations & Union Find Algo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import combinations | |
N,l = map(int,raw_input().split()) | |
sets = [] | |
idx = dict((i, i) for i in range(N)) | |
for i in xrange(l): | |
a,b = map(int,raw_input().split()) | |
val = idx[a] | |
for i in idx: | |
if idx[i] == val: | |
idx[i] = idx[b] | |
sets = {} | |
for val in set(idx.values()): | |
sets[val] = set([]) | |
for i in idx.keys(): | |
if idx[i] == val: | |
sets[val].add(i) | |
total = 0 | |
for g in combinations(sets.values(), 2): | |
total += len(g[0]) * len(g[1]) | |
print total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
N,l = map(int,raw_input().split())
sets = []
idx = dict((i, i) for i in range(N))
print idx
for i in xrange(l):
a,b = map(int,raw_input().split())
set_a = set([a])
set_b = set([b])