Алмаз Сейтхазин (КФ МГУ, кафедра НДС) DungeonMap 4350
Барлыбай Мади, 311/312 группа DungeonMap 4385
t1graph = {}t1graph = {}
2while True:2while True:
3    s = input().strip()3    s = input().strip()
4    if ' ' in s:4    if ' ' in s:
5        a, b = s.split()5        a, b = s.split()
6        if a not in graph:6        if a not in graph:
7            graph[a] = set()7            graph[a] = set()
8        graph[a].add(b)8        graph[a].add(b)
9        if b not in graph:9        if b not in graph:
10            graph[b] = set()10            graph[b] = set()
11        graph[b].add(a)11        graph[b].add(a)
12    else:12    else:
13        start = s13        start = s
14        end = input().strip()14        end = input().strip()
15        break15        break
16if start == end:16if start == end:
17    print('YES')17    print('YES')
18elif start not in graph:18elif start not in graph:
19    print('NO')19    print('NO')
20else:20else:
21    visited = set()21    visited = set()
22    stack = [start]22    stack = [start]
23    visited.add(start)23    visited.add(start)
24    found = False24    found = False
25    while stack:25    while stack:
26        node = stack.pop()26        node = stack.pop()
27        if node == end:27        if node == end:
28            found = True28            found = True
29            break29            break
30        for neighbor in graph[node]:30        for neighbor in graph[node]:
31            if neighbor not in visited:31            if neighbor not in visited:
32                visited.add(neighbor)32                visited.add(neighbor)
33                stack.append(neighbor)33                stack.append(neighbor)
34    print('YES' if found else 'NO')34    print('YES' if found else 'NO')
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op