Skip to content

Instantly share code, notes, and snippets.

@Kanol
Created March 19, 2020 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kanol/a74a72c20d1823f65e2ea967e1859235 to your computer and use it in GitHub Desktop.
Save Kanol/a74a72c20d1823f65e2ea967e1859235 to your computer and use it in GitHub Desktop.
def delete_single_edge():
for i in range(len(matrix)):
if sum(matrix[i]) <= 1:
for j in range(len(matrix[i])):
if matrix[i][j] == 1:
matrix[i][j] = 0
matrix[j][i] = 0
global isSomeEdgeDeleted
isSomeEdgeDeleted = True
break
isSomeEdgeDeleted = False
matrix = []
for i in range(int(input())):
matrix.append([int(x) for x in input().split()])
#matrix = [
# [0, 1, 0, 0, 0, 0, 0],
# [1, 0, 1, 0, 0, 0, 0],
# [0, 1, 0, 1, 1, 1, 0],
# [0, 0, 1, 0, 1, 0, 0],
# [0, 0, 1, 1, 0, 1, 0],
# [0, 0, 1, 0, 1, 0, 1],
# [0, 0, 0, 0, 0, 1, 0]
#]
delete_single_edge()
while isSomeEdgeDeleted:
isSomeEdgeDeleted = False
delete_single_edge()
path = []
start = -1
for i in range(len(matrix)):
index = -1
for j in range(len(matrix[i])):
if matrix[i][j] == 1:
index = j
if index == -1:
continue
else:
start = i
break
if start == -1:
print("A")
else:
while True:
path.append(start)
new_start = matrix[start].index(1)
if new_start in path:
path = path[path.index(new_start):]
break
else:
matrix[start][new_start] = 0
matrix[new_start][start] = 0
start = new_start
path = [x + 1 for x in path]
print("N")
print(sorted(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment