Задача Долой списывание def dfs now cnt for in now if cnt print NO exi

 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
Задача H. Долой списывание!
def dfs(now, cnt):
for e in a[now]:
if w[e] == cnt:
print("NO")
exit(0)
elif w[e] == 2:
w[e] = 1 - cnt
dfs(e, 1 - cnt)
return
n, m = map(int, input().split())
a = [0] * n
for i in range(n):
a[i] = []
for i in range(m):
v, u = map(int, input().split())
a[v - 1].append(u - 1)
a[u - 1].append(v - 1)
w = [2] * n
j = 0
while j < n:
if w[j] != 2:
j += 1
else:
dfs(j, 0)
w[j] = 0
print("YES")
Задача F. Получи дерево
n, m = map(int, input().split())
id = []
for i in range(n):
id.append(i)
for i in range(m):
a, b = map(int, input().split())
a -= 1
b -= 1
if id[a] != id[b]:
print(a + 1, b + 1)
x = id[a]
for j in range(n):
if id[j] == x:
id[j] = id[b]