SHARE
TWEET

Untitled

a guest Jun 16th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def find(u):
  2.     if parent[u] == u:
  3.         return u
  4.     parent[u] = find(parent[u])
  5.     return parent[u]
  6.  
  7. n,m = map(int,input().split())
  8. parent = [i for i in range(n+1)]
  9. ans = [0 for i in range(n+1)]
  10.  
  11. for i in range(1,m+1):
  12.     grp = [int(i) for i in input().split()][1:]
  13.     if len(grp) == 0:
  14.         continue
  15.     x = find(grp[0])
  16.     for i in grp[1:]:
  17.         parent[find(i)] = x
  18. for i in range(1,n+1):
  19.     ans[find(i)] += 1
  20. ans = " ".join(map(str,[ans[find(u)] for u in range(1, n + 1)]))
  21. print(ans)
  22.      
  23. from sys import stdin
  24. from sys import stdout
  25.  
  26. def find(u):
  27.     if parent[u] == u:
  28.         return u
  29.     parent[u] = find(parent[u])
  30.     return parent[u]
  31.  
  32. n, m = map(int, stdin.readline().split())
  33. parent = [i for i in range(n+1)]
  34. ans = [0 for i in range(1+n)]
  35. for i in range(1,m+1):
  36.     grp= [int(u) for u in stdin.readline().split()][1:]
  37.     if(len(grp)==0):
  38.         continue
  39.     x = find(grp[0])
  40.     for i in grp[1:]:
  41.         parent[find(i)] = x
  42.  
  43. for i in range(1,n+1):
  44.     ans[find(i)]+=1
  45. pprintit = ' '.join(map(str, [ans[find(u)] for u in range(1, n + 1)]))
  46. stdout.write('%s' % pprintit)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top