-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from collections import deque | ||
INF = 10**9 | ||
|
||
# with one color: | ||
def bfs(G, s): | ||
visited = [False] * len(G) | ||
vertex_queue = deque([s]) | ||
while vertex_queue: | ||
v = vertex_queue.popleft() | ||
if not visited[v]: | ||
visited[v] = True | ||
for u in G[v]: | ||
if not visited[u]: | ||
vertex_queue.append(u) | ||
return visited | ||
|
||
|
||
# with two colors for calculating path (+d, p): | ||
def bfs_p(G, s): | ||
visited = [False] * len(G) | ||
d = [INF] * len(G) | ||
p = [-1] * len(G) | ||
d[s] = 0 | ||
|
||
vertex_queue = deque([s]) | ||
while vertex_queue: | ||
v = vertex_queue.popleft() | ||
visited[v] = 2 | ||
for u in G[v]: | ||
if not visited[u]: | ||
visited[u] = 1 | ||
vertex_queue.append(u) | ||
d[u] = d[v] + 1 | ||
p[u] = v | ||
return d, p | ||
|
||
|
||
if __name__ == '__main__': | ||
N, M = map(int, input().split()) | ||
G = [[] for i in range(N)] | ||
|
||
for i in range(M): | ||
x, y = map(int, input().split()) | ||
G[x].append(y) | ||
G[y].append(x) | ||
s, f = map(int, input().split()) | ||
|
||
#searching shortest path s -> f: | ||
d, p = bfs_p(G, s) | ||
|
||
if d[f] == INF: | ||
print(-1) | ||
else: | ||
print(d[f]) | ||
path = [] | ||
i = f | ||
while i != s: | ||
path.append(i) | ||
i = p[i] | ||
path.append(s) | ||
print(' '.join(map(str, path[::-1]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from itertools import groupby | ||
#import sys | ||
#sys.setrecursionlimit(640000000) | ||
|
||
|
||
# just traverses all vertices reachable from v: | ||
def dfs(G, visited, v): | ||
visited[v] = True | ||
for n in G[v]: | ||
if not visited[n]: | ||
dfs(G, visited, n) | ||
|
||
|
||
# just traverses all vertices reachable from v: | ||
def dfs_nonrec_1(G, visited, s): | ||
vertex_stack = [s] | ||
while vertex_stack: | ||
v = vertex_stack.pop() | ||
if not visited[v]: | ||
visited[v] = True | ||
for u in G[v]: | ||
if not visited[u]: | ||
vertex_stack.append(u) | ||
|
||
|
||
# allows to get a moment when we color vertex with blue: | ||
# (like ending of executing dfs(v) for recursive case) | ||
def dfs_nonrec_2(G, visited, s): | ||
vertex_stack = [s] | ||
while vertex_stack: | ||
v = vertex_stack[-1] | ||
if not visited[v]: | ||
visited[v] = True | ||
for u in G[v]: | ||
if not visited[u]: | ||
vertex_stack.append(u) | ||
else: | ||
# all neighbours of v were visited | ||
vertex_stack.pop() | ||
|
||
|
||
# checks for cycles in undirected graph: | ||
def dfs_cycles_undirected(G, visited, v, p=-1): | ||
visited[v] = True | ||
for u in G[v]: | ||
if not visited[u]: | ||
dfs_cycles_undirected(G, visited, u, v) | ||
else: | ||
if u != p: | ||
print('Cycle found!') | ||
|
||
|
||
def dfs_cycles_undirected_nonrec(G, visited, s): | ||
vertex_stack = [(s, -1)] | ||
while vertex_stack: | ||
v, p = vertex_stack[-1] | ||
if not visited[v]: | ||
visited[v] = True | ||
for u in G[v]: | ||
if not visited[u]: | ||
vertex_stack.append((u, v)) | ||
else: | ||
if u != p: | ||
print('Cycle found!') | ||
else: | ||
# all neighbours of v were visited | ||
vertex_stack.pop() | ||
|
||
|
||
# checks for cycles in directed graph: | ||
def dfs_cycles_directed(G, visited, v): | ||
visited[v] = 1 | ||
for u in G[v]: | ||
if not visited[u]: | ||
dfs(G, visited, u) | ||
elif visited[u] == 1: | ||
print('Cycle found!') | ||
visited[v] = 2 | ||
|
||
|
||
def dfs_cycles_directed_nonrec(G, visited, s): | ||
vertex_stack = [s] | ||
while vertex_stack: | ||
v = vertex_stack[-1] | ||
if not visited[v]: | ||
visited[v] = 1 | ||
for u in G[v]: | ||
if not visited[u]: | ||
vertex_stack.append(u) | ||
elif visited[u] == 1: | ||
print('Cycle found!') | ||
else: | ||
# all neighbours of v were visited | ||
visited[v] = 2 | ||
vertex_stack.pop() | ||
|
||
|
||
if __name__ == '__main__': | ||
N, M = map(int, input().split()) | ||
G = [[] for i in range(N)] | ||
|
||
for i in range(M): | ||
x, y = map(int, input().split()) | ||
G[x].append(y) | ||
G[y].append(x) | ||
|
||
# calculating number of connected components: | ||
|
||
visited = [False] * len(G) | ||
n_components = 0 | ||
for i in range(N): | ||
if not visited[i]: | ||
n_components += 1 | ||
dfs(G, visited, i) | ||
|
||
print(n_components) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import sys | ||
|
||
|
||
def dfs(G, visited, topsort, v): | ||
visited[v] = 1 | ||
for u in G[v]: | ||
if not visited[u]: | ||
dfs(G, visited, topsort, u) | ||
elif visited[u] == 1: | ||
print('No') | ||
sys.exit(0) | ||
visited[v] = 2 | ||
topsort.append(v) | ||
|
||
|
||
def dfs_nonrec(G, visited, topsort, s): | ||
vertex_stack = [s] | ||
while vertex_stack: | ||
v = vertex_stack[-1] | ||
if not visited[v]: | ||
visited[v] = 1 | ||
for u in G[v]: | ||
if not visited[u]: | ||
vertex_stack.append(u) | ||
elif visited[u] == 1: | ||
print('No') | ||
sys.exit(0) | ||
else: | ||
# all neighbours of v were visited | ||
if visited[v] == 1: | ||
topsort.append(v) | ||
visited[v] = 2 | ||
vertex_stack.pop() | ||
|
||
|
||
if __name__ == '__main__': | ||
N, M = map(int, input().split()) | ||
G = [[] for i in range(N)] | ||
|
||
for i in range(M): | ||
x, y = map(int, input().split()) | ||
G[x].append(y) | ||
|
||
visited = [False] * len(G) | ||
|
||
topsort = [] | ||
for i in range(N): | ||
if not visited[i]: | ||
dfs_nonrec(G, visited, topsort, i) | ||
|
||
topsort = [v for v in topsort] | ||
|
||
print('Yes') | ||
print(' '.join(map(str, topsort[::-1]))) |