l2-013 红色警报python
时间: 2023-12-12 17:35:43 浏览: 82
根据题目要求,我们需要编写一个报警程序,当***警报。如果该国失去了最后一个城市,则增加一行输出Game Over。
以下是Python实现的代码:
```python
from collections import defaultdict
n, m = map(int, input().split())
graph = defaultdict(list)
for i in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
lost_city = set()
for i in range(int(input())):
city = int(input())
lost_city.add(city)
visited = set()
for node in graph:
if node not in lost_city and node not in visited:
stack = [node]
while stack:
cur = stack.pop()
visited.add(cur)
for neighbor in graph[cur]:
if neighbor not in lost_city and neighbor not in visited:
stack.append(neighbor)
if len(visited) != n - len(lost_city):
print("Red Alert: City {} is lost!".format(city))
else:
print("City {} is lost.".format(city))
if len(lost_city) == n:
print("Game Over.")
```