京东数据岗第二题,有向无环图,暴力求解怎么只过9%?
import itertools n,m=map(int,input().split()) arr=[] for i in range(m): edge=list(map(int,input().split())) arr.append(edge) pl=[] for item in itertools.permutations([i+1 for i in range(n)]): pl.append(item) flg=True i=0 while i<len(pl)and flg: flag=True for j in range(m): if pl[i][arr[j][0]-1]>pl[i][arr[j][1]-1]: flag=False break if flag: ans=pl[i] flg=False i+=1 for k in range(n): if k==n-1: print(ans[k]) else: print(ans[k],end=' ')
#京东##笔试题目##实习##春招#