#!/usr/bin/python3.9 from itertools import permutations n = 4 l = [0, 1,2,3,4] class node: def __init__(self,nr): self.nr = nr self.edge = [] def addEdge(self,nr): self.edge.append(nr) def print(self): print("nr="+str(self.nr),end=" (") print(",".join( map(str,self.edge)),end="") print(")") t = [ node(0) ] def floyd(): for i in range(1, n+1): t[i].d = [900000 for j in range(n+1)] t[i].d[i] = 0 t[i].d[0] = 0 for i in range(1, n+1): for j in t[i].edge: t[i].d[j] = 1 for i in range(1, n+1): for j in range(1, n+1): for k in range(1, n+1): if t[j].d[k] > t[j].d[i] + t[i].d[k]: t[j].d[k] = t[j].d[i] + t[i].d[k] def print_t(): for i in range(1, n+1): t[i].print() def print_d(): print(" ",end="") for i in range(1, n+1): print(str(i),end=" ") print() for i in range(1, n+1): print(str(i),end=" ") for j in range(1, n+1): if (t[i].d[j] == 900000): print("x",end=" ") else: print( str(t[i].d[j]), end=" ") print() def print_res(): res = [] for i in range(1, n+1): res.append(sum(map(lambda x: 0 if x == 900000 else x, t[i].d))) print(" ".join(map(str,res))) n = int(input()) x = map(int, input().split()) l = [0] for a in x: l.append(a) for i in range(1,n+1): nd = node(i) for j in range(1,n+1): if i < j and l[i] > l[j]: nd.addEdge(j) if i > j and l[i] < l[j]: nd.addEdge(j) t.append(nd) floyd() print_res()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #!/usr/bin/python3.9 from itertools import permutations n = 4 l = [0, 1,2,3,4] class node: def __init__(self,nr): self.nr = nr self.edge = [] def addEdge(self,nr): self.edge.append(nr) def print(self): print("nr="+str(self.nr),end=" (") print(",".join( map(str,self.edge)),end="") print(")") t = [ node(0) ] def floyd(): for i in range(1, n+1): t[i].d = [900000 for j in range(n+1)] t[i].d[i] = 0 t[i].d[0] = 0 for i in range(1, n+1): for j in t[i].edge: t[i].d[j] = 1 for i in range(1, n+1): for j in range(1, n+1): for k in range(1, n+1): if t[j].d[k] > t[j].d[i] + t[i].d[k]: t[j].d[k] = t[j].d[i] + t[i].d[k] def print_t(): for i in range(1, n+1): t[i].print() def print_d(): print(" ",end="") for i in range(1, n+1): print(str(i),end=" ") print() for i in range(1, n+1): print(str(i),end=" ") for j in range(1, n+1): if (t[i].d[j] == 900000): print("x",end=" ") else: print( str(t[i].d[j]), end=" ") print() def print_res(): res = [] for i in range(1, n+1): res.append(sum(map(lambda x: 0 if x == 900000 else x, t[i].d))) print(" ".join(map(str,res))) n = int(input()) x = map(int, input().split()) l = [0] for a in x: l.append(a) for i in range(1,n+1): nd = node(i) for j in range(1,n+1): if i < j and l[i] > l[j]: nd.addEdge(j) if i > j and l[i] < l[j]: nd.addEdge(j) t.append(nd) floyd() print_res() |