Skip to content

Instantly share code, notes, and snippets.

@201411108
Last active July 27, 2021 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 201411108/1f530465b8d8735d537fb4b29037212f to your computer and use it in GitHub Desktop.
Save 201411108/1f530465b8d8735d537fb4b29037212f to your computer and use it in GitHub Desktop.
in acmicpc, undirected graph input methods
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