Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions khudiakov/A1_components.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

import sys
import threading



def main():

# read number of vertices
f = open('components.in')
first_string = f.readline().strip().split(' ')
n = int(first_string[0])

#read edges list
next_string = ' '
edges_list = []
inner_list = []
while next_string != ['']:
next_string = f.readline().strip().split(' ')
if next_string != ['']:
inner_list.append(int(next_string[0])-1)
inner_list.append(int(next_string[1])-1)
edges_list.append(inner_list)
inner_list = []
f.close()

# make full edges list with reverse edges
rev_edges_list = []
for i in edges_list:
a = i[::-1]
rev_edges_list.append(a)
full_edges_list = edges_list + rev_edges_list

#convert edges list to adjacency list
adjacency_list = []

for i in range(n):
adjacency_list.append([])

for i in full_edges_list:
a = (i[0])
adjacency_list[a].append(i[1])

#dfs
visited = [False] * n
comp_number = 0
components_list = [-1] * n

def dfs(v):
components_list[v] = comp_number
visited[v] = True
for w in adjacency_list[v]:
if not visited[w]:
dfs(w)

#counting components
for v in range(n):
if not visited[v]:
dfs(v)
comp_number += 1


#write to file

ans = open('components.out', 'w')
ans.write(str(comp_number) + '\n')
for w in components_list:
ans.write(str(int(w) + 1) + ' ')
ans.close()


threading.stack_size(2 ** 26) # 64 MB stack size
sys.setrecursionlimit(1000000000) # recursion depth
thread = threading.Thread(target=main)
thread.start()
50 changes: 50 additions & 0 deletions khudiakov/A2_Shortest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

import sys
import threading

def main():

# read number of vertices
f = open('pathbge1.in')
v, e = (int(i) for i in f.readline().strip().split())


#adjacency list
adjacency_list=[[] for i in range(v)]
for edge in f:
x, y = ((int(i) - 1) for i in edge.strip().split())
adjacency_list[x].append(y)
adjacency_list[y].append(x)

#bfs
visited = [False] * v
distance = [-1 for i in range(v)]

def bfs(start, adjacency_list):
queue = [start]
visited[start] = True
distance[start] = 0 # distance from start vertex to current one
while len(queue) > 0:

x = queue.pop(0)

for i in adjacency_list[x]:
if visited[i] == False:
queue.append(i)
visited[i] = True
if distance[i] == -1:
distance[i] = distance[x] + 1
return distance

#write to file

ans = open('pathbge1.out', 'w')
for w in bfs(0, adjacency_list):
ans.write(str(int(w)) + ' ')
ans.close()


threading.stack_size(2 ** 26) # 64 MB stack size
sys.setrecursionlimit(1000000000) # recursion depth
thread = threading.Thread(target=main)
thread.start()
45 changes: 45 additions & 0 deletions khudiakov/A3_Shortest_with_weight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import sys
sys.setrecursionlimit(1000000000)

#read number of vertices, star and stop points from file
file = open('pathmgep.in', 'r')

n, s, f = [int(i) for i in file.readline().split()]
s-= 1
f-= 1

#read weight matrix
path = []
for string in file.read().splitlines():
weight = [int(i) for i in string.split()]
path.append(weight)
file.close()

#mark nonexistent paths
for i in range(n):
for j in range(n):
if path[i][j] == -1:
path[i][j] = '-1'

#may be slow but..
def FloydWarshall(path):
for k in range(n):
for i in range(n):
for j in range(n):
if path[i][j] != '-1'and path[i][k] != '-1'and path[k][j] != '-1':
path[i][j] = min(path[i][j], (path[i][k] + path[k][j]))
elif path[i][j] != '-1'and (path[i][k] == '-1'or path[k][j] == '-1'):
path[i][j] = path[i][j]
elif path[i][j] == '-1'and path[i][k] != '-1'and path[k][j] != '-1':
path[i][j] = path[i][k] + path[k][j]
else:
path[i][j] = '-1'
return path

FloydWarshall(path)


ans = open('pathmgep.out', 'w')
ans.write(str(path[s][f]))
ans.close()
Loading