ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 16583 |
문제명 | Boomerangs |
레벨 | 플래티넘 1 |
분류 |
DFS |
시간복잡도 | O(V+E) |
인풋사이즈 | V<=100,000, E<=100,000 |
사용한 언어 | Python 3.11 |
제출기록 | 78024KB / 740ms |
최고기록 | 704ms |
해결날짜 | 2023/02/27 |
"""Solution code for "BOJ 16583. Boomerangs".
- Problem link: https://www.acmicpc.net/problem/16583
- Solution link: http://www.teferi.net/ps/problems/boj/16583
Tags: [DFS]
"""
import sys
from teflib import graph as tgraph
def main():
N, M = [int(x) for x in sys.stdin.readline().split()]
graph = [set() for _ in range(N)]
for _ in range(M):
u, v = [int(x) for x in sys.stdin.readline().split()]
graph[u - 1].add(v - 1)
graph[v - 1].add(u - 1)
answers = []
prev = [None] * N
children = [[] for _ in range(N)]
for source in range(N):
for u, is_discovering in tgraph.full_dfs(graph, source, prev=prev):
p = prev[u]
if is_discovering:
for v in graph[u]:
if v != p and prev[v] is not None:
children[u].append(v)
else:
children_u = children[u]
for i in range(0, len(children_u) - 1, 2):
answers.append((children_u[i], u, children_u[i + 1]))
if p != u:
if len(children_u) % 2 == 1:
answers.append((p, u, children_u[-1]))
else:
children[p].append(u)
print(len(answers))
for answer in answers:
print(*(x + 1 for x in answer))
if __name__ == '__main__':
main()