Skip to content

Commit 09c67d9

Browse files
committed
Add Eulers path problem
1 parent 0524e3a commit 09c67d9

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Seminars/sem_15/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898

9999
## Задачи за упражнение
100100

101+
- [The Islander](https://www.hackerrank.com/contests/sda-hw-13-2022/challenges/islander/problem)
101102
- [Permutations](https://leetcode.com/problems/permutations)
102103
- [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target)
103104
- [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary)

Tasks/tasks_15/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Условия:
22

3+
- [The Islander](https://www.hackerrank.com/contests/sda-hw-13-2022/challenges/islander/problem)
34
- [Permutations](https://leetcode.com/problems/permutations)
45
- [All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target)
56
- [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def dfs(current, visited, graph):
2+
for neighbor in graph[current]:
3+
if neighbor not in visited:
4+
visited.add(neighbor)
5+
dfs(neighbor, visited, graph)
6+
7+
8+
def is_connected(V, graph):
9+
start_vertex = 1
10+
visited = set([start_vertex])
11+
12+
dfs(start_vertex, visited, graph)
13+
14+
return len(visited) >= V
15+
16+
17+
def check_euler(V, graph):
18+
degrees = [0] * (V + 1)
19+
20+
for v in graph:
21+
degrees[v] = len(graph[v])
22+
23+
count_odd = sum(1 if degree % 2 == 1 else 0 for degree in degrees)
24+
25+
if not is_connected(V, graph):
26+
return 'none'
27+
28+
if count_odd == 0:
29+
return 'ecycle'
30+
31+
if count_odd == 2:
32+
return 'epath'
33+
34+
return 'none'
35+
36+
37+
def solve():
38+
N, M = map(int, input().split())
39+
graph = {v: [] for v in range(N + 1)}
40+
41+
for _ in range(M):
42+
x, y = map(int, input().split())
43+
graph[x].append(y)
44+
graph[y].append(x)
45+
46+
result = check_euler(N, graph)
47+
48+
return result
49+
50+
Q = int(input())
51+
for _ in range(Q):
52+
print(solve())

0 commit comments

Comments
 (0)