|
| 1 | +import sys |
| 2 | +from collections import defaultdict, deque |
| 3 | + |
| 4 | +read = lambda: sys.stdin.readline().rstrip() |
| 5 | + |
| 6 | + |
| 7 | +class Problem: |
| 8 | + def __init__(self): |
| 9 | + self.v, self.e = map(int, read().split()) |
| 10 | + |
| 11 | + self.graph = defaultdict(list[int]) |
| 12 | + for x, y in [map(int, read().split()) for _ in range(self.e)]: |
| 13 | + self.graph[x].append(y) |
| 14 | + self.graph[y].append(x) |
| 15 | + |
| 16 | + def solve(self) -> None: |
| 17 | + discovered, low, points, order = ( |
| 18 | + [-1] * (self.v + 1), |
| 19 | + [-1] * (self.v + 1), |
| 20 | + set(), |
| 21 | + 0, |
| 22 | + ) |
| 23 | + |
| 24 | + for node in range(1, self.v + 1): |
| 25 | + if discovered[node] == -1: |
| 26 | + order = self.dfs(node, discovered, low, points, order) |
| 27 | + |
| 28 | + print(len(points)) |
| 29 | + print(*sorted(list(points))) |
| 30 | + |
| 31 | + def dfs( |
| 32 | + self, |
| 33 | + start_node: int, |
| 34 | + discovered: list[int], |
| 35 | + low: list[int], |
| 36 | + points: set[int], |
| 37 | + order: int, |
| 38 | + ) -> int: |
| 39 | + stack, root_children_count = deque([(start_node, 0, iter(self.graph[start_node]))]), 0 |
| 40 | + |
| 41 | + while stack: |
| 42 | + current_node, parent, neighbors = stack[-1] |
| 43 | + |
| 44 | + if discovered[current_node] == -1: |
| 45 | + order += 1 |
| 46 | + discovered[current_node] = low[current_node] = order |
| 47 | + |
| 48 | + for neighbor in neighbors: |
| 49 | + if neighbor == parent: |
| 50 | + continue |
| 51 | + |
| 52 | + if discovered[neighbor] != -1: |
| 53 | + low[current_node] = min(low[current_node], discovered[neighbor]) |
| 54 | + else: |
| 55 | + if parent == 0: |
| 56 | + root_children_count += 1 |
| 57 | + stack.append((neighbor, current_node, iter(self.graph[neighbor]))) |
| 58 | + break |
| 59 | + else: |
| 60 | + stack.pop() |
| 61 | + if parent != 0: |
| 62 | + low[parent] = min(low[parent], low[current_node]) |
| 63 | + |
| 64 | + if parent != start_node and low[current_node] >= discovered[parent]: |
| 65 | + points.add(parent) |
| 66 | + |
| 67 | + if root_children_count > 1: |
| 68 | + points.add(start_node) |
| 69 | + |
| 70 | + return order |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + Problem().solve() |
0 commit comments