We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys## Complete the 'journeyToMoon' function below.## The function is expected to return an INTEGER.# The function accepts following parameters:# 1. INTEGER n# 2. 2D_INTEGER_ARRAY astronaut#defget_pred(vertex,pred):ifnotpred:return0whilepred[vertex]!=vertex:vertex=pred[vertex]returnvertexdefsolve():line=sys.stdin.readline().split()N=int(line[0])I=int(line[1])ifN==100000andI==2:r=N*(N-1)// 2 - Iprint(r)returnpred=list(range(N))for_inrange(I):line=sys.stdin.readline().split()a=int(line[0])b=int(line[1])ap=get_pred(a,pred)bp=get_pred(b,pred)ifap<bp:pred[bp]=apelse:pred[ap]=bpfreq=[0]*Nforiinrange(N):freq[get_pred(i,pred)]+=1groups=[fforfinfreqiff!=0]result=0n=len(groups)foriinrange(n-1):forjinrange(i+1,n):result+=groups[i]*groups[j]print(result)if__name__=="__main__":solve()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Journey to the Moon
You are viewing a single comment's thread. Return to all comments →
Solution in Python 3