-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
30-Dolchae #112
30-Dolchae #112
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DFSλ‘ νΈλκΉ κ°κ²°νκ² μ½λκ° μ§μ§λκ΅°μ...π
μ λ BFSλ‘ λμ νλλ° μ½λκ° 1000Bκ° λμλ€μ...νν
κΉλνκ² μ νΈμ
μ μ μ½λλ§ λ¨κΈ°κ³ κ°κ² μ΅λλ€!
BFS μ½λ
from collections import deque
input = open(0).readline
people_num = int(input())
A, B = map(int, input().split())
relationship = [[0] for _ in range(people_num + 1)]
for i in range(relation_num := int(input())):
a, b = map(int, input().split())
if relationship[a] == [0]:
relationship[a] = [b]
else:
relationship[a].append(b)
if relationship[b] == [0]:
relationship[b] = [a]
else:
relationship[b].append(a)
visited = [0 for _ in range(people_num + 1)]
def chon(r, c, p):
global ans, check
q = deque()
q.append((r, c, p))
while q:
relation, cnt, person = q.popleft()
if visited[person] == 0:
visited[person] = 1
cnt += 1
for j in relation[person]:
if j == A:
ans.append(cnt)
return ans
q.append((relation, cnt, j))
ans = []
cnt = 0
answer = chon(relationship, cnt, B)
print(min(answer) if answer else -1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ μμ λͺ¨λ λ Έλμ λν 거리 μ 보λ₯Ό κΈ°λ‘νλ 리μ€νΈλ₯Ό νλ λ§λ€μ΄μ, κ·Έ κ°μ κ³μ λ°κΏμ£Όλ μμΌλ‘ μ§λ΄€λ€μ.
input = open(0).readline
n = int(input())
a, b = map(int, input().split())
m = int(input())
tree = [[] for _ in range(n + 1)]
dists = [-1] * (n + 1) # μ΄κΈ°κ°μ -1
dists[a] = 0 # μμ λ
ΈλμΈ aλ 거리 0μΌλ‘ μ€μ
for _ in range(m):
x, y = map(int, input().split())
tree[x].append(y)
tree[y].append(x)
def dfs(src: int = a):
for dst in tree[src]: # srcμ μ°κ²°λ μΈμ λ
Έλμ λν΄
if dists[dst] == -1: # μ΄κΈ°κ°(-1) κ·Έλλ‘λΌλ©΄ λ°©λ¬Έν μ μμ
dists[dst] = dists[src] + 1 # src λ
ΈλκΉμ§μ 거리 + 1λ‘ μ¬μ€μ
dfs(dst) # μ¬κ· νΈμΆ
dfs()
print(dists[b]) # μ΅μ’
μ μΌλ‘ bμ λν 거리κ°μ μΆλ ₯νλ©΄ λ¨. λλ¬νμΌλ©΄ 거리κ°μ΄, λλ¬λͺ»νμΌλ©΄ μ΄κΈ° -1κ°μ΄ κ·Έλλ‘ λ΄κ²¨μ Έμμ
κ·Έλ¦¬κ³ λ°λμ μ¬κ·λ‘ ν νμλ μκ² μ£ ? stackμ μ΄μ©ν dfsλ κ°λ₯νλ΅λλ€.
input = open(0).readline
n = int(input())
a, b = map(int, input().split())
m = int(input())
tree = [[] for _ in range(n + 1)]
dists = [-1] * (n + 1)
dists[a] = 0
for _ in range(m):
x, y = map(int, input().split())
tree[x].append(y)
tree[y].append(x)
stack = [a]
while stack:
src = stack.pop()
if src == b:
break
for dst in tree[src]:
if dists[dst] == -1:
dists[dst] = dists[src] + 1
stack.append(dst)
print(dists[b])
λ§μΉ queueλ₯Ό μ΄ bfsλ μ½λκ° λΉμ·νμ£ ? μλκ° bfs μ½λμ λλ€.
from collections import deque
input = open(0).readline
n = int(input())
a, b = map(int, input().split())
m = int(input())
tree = [[] for _ in range(n + 1)]
dists = [-1] * (n + 1)
dists[a] = 0
for _ in range(m):
x, y = map(int, input().split())
tree[x].append(y)
tree[y].append(x)
q = deque([a])
while q:
src = q.popleft()
if src == b:
break
for dst in tree[src]:
if dists[dst] == -1:
dists[dst] = dists[src] + 1
q.append(dst)
print(dists[b])
νΉμ΄νκ² μ΄ λ¬Έμ λ bfsκ° μλκ° μ μΌ λ리λ€μ
맨 λ°μ΄ μ¬κ·νΈμΆ, κ°μ΄λ°κ° stack dfs, 맨 μκ° queue bfs μ½λμ
λλ€.
def dfs(v, num): | ||
num += 1 | ||
visited[v] = True | ||
|
||
if v == b: | ||
result.append(num) | ||
|
||
for i in graph[v]: | ||
if not visited[i]: | ||
dfs(i, num) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
거리κ°(μ΄ μ)μ numμ΄ κΈ°μ΅νκ³ μμΌλ, μ΄λ₯Ό λ°ννλ μμΌλ‘ μ½λλ₯Ό μ§λ©΄ μ’ λ μ§κ΄μ μΌ κ² κ°λ€μ.
def dfs(src: int = a, dist: int = 0): # λν΄νΈ νλΌλ―Έν°λ‘ λ
Έλ a, 거리 0μ μ§μ ν¨
if visited[src]: # λ°©λ¬Ένλ μ μ μ΄λΌλ©΄
return -1 # μ°Ύλ λμμ΄ μλλ―λ‘ -1 리ν΄
elif src == b: # νμ¬ κ²μ¬νλ λ
Έλκ° μ°Ύλ λμ(b)μ΄λΌλ©΄
return dist # νμ¬κΉμ§μ 거리κ°μ λ°ν
else:
visited[src] = True # λ°©λ¬Έμ²λ¦¬
ans = -1
for dst in tree[src]: # μΈμ ν λ
Έλλ€μ λν΄
ans = max(ans, dfs(dst, dist + 1)) # μ¬κ· νΈμΆμ λν κ²°κ³Ό μ€ ν° κ°μ μ·¨ν¨
return ans # ν° κ°μ μ€μ κ±°λ¦¬μΌ κ±°κ³ , μλλΌλ©΄ λͺ»μ°Ύμλ€λ μλ―Έμ -1μ΄ λ΄κ²¨μμ
μ΄λ° μμΌλ‘ μμ±νλ©΄, dfs ν¨μλ₯Ό νΈμΆνμ λ μ€μ 거리 κ° μ΄ λ°νλκ±°λ λͺ»μ°Ύμλ€λ λ»μμ -1 μ΄ λ°νλκ² μ£ ?
κ·Έκ±°λ₯Ό λ°λ‘ μΆλ ₯ν΄μ£Όλ©΄ λ©λλ€.
print(dfs()) # dfs ν¨μμ λν΄νΈ νλΌλ―Έν°λ₯Ό μ§μ ν΄λμ μΈμλ₯Ό λκ²¨μ£Όμ§ μμλ λ©λλ€.
π λ¬Έμ λ§ν¬
2644 μ΄μκ³μ°
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
λ¬Έμ
μ£Όμ΄μ§ λ μ¬λμ μ΄μλ₯Ό κ³μ°νλ λ¬Έμ μ΄λ€.
μ λ ₯
μΆλ ₯
μμ 1)
μ½λ
aμμ bλ‘ κ°λ dfsλ₯Ό μννλ€.
μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
ꡬκ΅ν© μ λ°°λμ΄ μΆμ²ν΄μ£Όμ λ°±νΈλνΉ λ¬Έμ λ₯Ό νμ΄λ³΄λ € νλλ° μ°Ύμ보λ λ°±νΈλνΉμ DFSκ° μ°μΈλ€κΈΈλ DFS λ¬Έμ λ₯Ό λ¨Όμ λμ ν΄λ³΄μλ€. κ·Έλ°λ° μ€λλ§μ νΈλ λλμ± λͺ¨λ₯΄κ² μ΄μ μ λ¨Ήλ€κ° κ²°κ΅ λΈλ‘κ·Έ μ°Έκ³ νλ€..
μ½λ μ°Έκ³ λΈλ‘κ·Έ