Last active
July 27, 2021 01:52
-
-
Save 201411108/1f530465b8d8735d537fb4b29037212f to your computer and use it in GitHub Desktop.
in acmicpc, undirected graph input methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
n, m, v = list(map(int, sys.stdin.readline().split())) # N개의 정점, M개의 간선, 시작 노드 V | |
graph = [[] for _ in range(n + 1)] # n + 1인 이유 -> 편의상, 문제에 주어지는 값 그대로를 사용하기 위해 | |
"""dictionary 사용 시 | |
graph = collections.defaultdict(list) # 단 이 경우, 노드를 조회할 때 list(graph) 형태로 해줘야 에러가 발생하지 않는다. | |
""" | |
# 양방향 그래프 일 경우 | |
for _ in range(m): | |
n, e = map(int, input().split()) | |
graph[n].append(e) | |
graph[e].append(n) | |
""" 단방향 그래프일 경우 | |
for _ in range(m): | |
n, e = map(int, input().split()) | |
graph[n].append(e) | |
""" | |
for i in graph: | |
i.sort() # 노드의 순서가 중요한 경우 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment