๊ธฐ๋ณธ๊ธฐ ๋ฌธ์ ๋ฆฌ์คํธ
๊ธฐ๋ณธ๊ธฐ๋ฅผ ๋ค์ง๊ธฐ ์ํ ๋๋ฆด ๋ฃจํด์ผ๋ก ์ฌ์ฉํ๋ ๋ฌธ์ ๋ค์ด๋ค.
์ด 8๊ฐ๋ก ๊ฐ ๋ฌธ์ ์ ์ฃผ์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
1. DFS์ BFS
2. ํต์ ๋ ฌ
3. ์ด์งํ์
4. ๋ค์ต์คํธ๋ผ
5. ํ๋ก์ด๋ ์์
6. ์๋ก์ ์งํฉ
7. ํฌ๋ฃจ์ค์นผ
8. ์์์ ๋ ฌ
๋ฌธ์ ๋งํฌ์ ์ ๋ต ์ฝ๋
1. DFS์ BFS
https://www.acmicpc.net/problem/1260
1260๋ฒ: DFS์ BFS
์ฒซ์งธ ์ค์ ์ ์ ์ ๊ฐ์ N(1 ≤ N ≤ 1,000), ๊ฐ์ ์ ๊ฐ์ M(1 ≤ M ≤ 10,000), ํ์์ ์์ํ ์ ์ ์ ๋ฒํธ V๊ฐ ์ฃผ์ด์ง๋ค. ๋ค์ M๊ฐ์ ์ค์๋ ๊ฐ์ ์ด ์ฐ๊ฒฐํ๋ ๋ ์ ์ ์ ๋ฒํธ๊ฐ ์ฃผ์ด์ง๋ค. ์ด๋ค ๋ ์ ์ ์ฌ
www.acmicpc.net
from collections import deque
input_n, input_m, input_v = map(int,input().split())
graph_list=[[] for _ in range(input_n+1)]
for i in range(input_m):
node1, node2 = map(int, input().split())
target_idx=0
while target_idx<len(graph_list[node1]):
if graph_list[node1][target_idx]>=node2: break
target_idx+=1
graph_list[node1]=graph_list[node1][:target_idx] + [node2] + graph_list[node1][target_idx:]
target_idx=0
while target_idx<len(graph_list[node2]):
if graph_list[node2][target_idx]>=node1: break
target_idx+=1
graph_list[node2]=graph_list[node2][:target_idx] + [node1] + graph_list[node2][target_idx:]
dfs_history=[]
bfs_history=[]
def dfs(graph, v, visited):
global dfs_history
visited[v]= True
dfs_history.append(v)
for i in graph[v]:
if not visited[i]:
dfs(graph, i, visited)
def bfs(graph, v, visited):
global bfs_history
visited[v]=True
queue=deque([v])
while queue:
cur_node=queue.popleft()
bfs_history.append(cur_node)
for i in graph[cur_node]:
if not visited[i]:
queue.append(i)
visited[i]=True
dfs(graph_list, input_v, [False for _ in range(input_n+1)])
bfs(graph_list, input_v, [False for _ in range(input_n+1)])
for i in dfs_history:
print("%d "%i, end="")
print()
for i in bfs_history:
print("%d "%i, end="")
๊ฐ์ ์ ๋ณด๋ฅผ ์ธ์ ๋ฆฌ์คํธ์ ๋ด์ ๋, ์ค๋ฆ์ฐจ์์ผ๋ก ๋ด๊ธฐ ์ํด ์ ํํ์ ์๊ณ ๋ฆฌ์ฆ์ด ํฌํจ๋์๋ค.
์ด๋ถ๋ถ์ ์ด์งํ์์ ํ์ฉํ๊ฑฐ๋ ํ ์๋ฃํ์ ํ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๊ฐ์ ๊ฐ๋ฅํ๋ค.
2. ํต์ ๋ ฌ
https://www.acmicpc.net/problem/2750
2750๋ฒ: ์ ์ ๋ ฌํ๊ธฐ
์ฒซ์งธ ์ค์ ์์ ๊ฐ์ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์๋ ์ ์ฃผ์ด์ง๋ค. ์ด ์๋ ์ ๋๊ฐ์ด 1,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์ ์์ด๋ค. ์๋ ์ค๋ณต๋์ง ์๋๋ค.
www.acmicpc.net
import sys
sys.setrecursionlimit(15000)
input_n=int(input())
li=[]
for i in range(input_n):
li.append(int(input()))
def QuickSort(array, start, end):
if start>=end: return
pivot=start
left=start+1
right=end
while left<=right:
while left<=end and array[left]<=array[pivot]:
left+=1
while right>start and array[right]>=array[pivot]:
right-=1
if right<left:
array[right], array[pivot]=array[pivot], array[right]
else:
array[left],array[right]=array[right], array[left]
QuickSort(array,start,right-1)
QuickSort(array,right+1,end)
QuickSort(li, 0, len(li)-1)
for i in li:
print(i)
์ฌ๊ท ์ ํ์ ํ์ด์ฃผ์ด์ผ ํ๋ค.
๋ถ๋ฑํธ์์ ๋ฑํธ์ ํฌํจ ์ฌ๋ถ, ์กฐ๊ฑด์ ์์ ๋ฑ ์ ๊ฒฝ ์ธ ๋ถ๋ถ์ด ๋ง์ ์ฝ๋์ด๋ ์ฃผ์ํ์.
๋ํ ์ฐ์ต๋ left์ start, right์ end๋ฅผ ํผ๋ํ์ฌ ์ฐ๋ ์ค์๊ฐ ์์์ผ๋ ์ฃผ์ํ์.
3. ์ด์งํ์
https://www.acmicpc.net/problem/1920
1920๋ฒ: ์ ์ฐพ๊ธฐ
์ฒซ์งธ ์ค์ ์์ฐ์ N(1 ≤ N ≤ 100,000)์ด ์ฃผ์ด์ง๋ค. ๋ค์ ์ค์๋ N๊ฐ์ ์ ์ A[1], A[2], …, A[N]์ด ์ฃผ์ด์ง๋ค. ๋ค์ ์ค์๋ M(1 ≤ M ≤ 100,000)์ด ์ฃผ์ด์ง๋ค. ๋ค์ ์ค์๋ M๊ฐ์ ์๋ค์ด ์ฃผ์ด์ง๋๋ฐ, ์ด ์๋ค
www.acmicpc.net
def Binary_Search(array, target, start, end):
while start<=end:
#print("looping..")
mid = (start+end)//2
if array[mid]==target:
return mid
elif target<array[mid]:
end=mid-1
else:
start=mid+1
return None
N=int(input())
array=list(map(int,input().split()))
array.sort()
M=int(input())
targets=list(map(int,input().split()))
for i in range(M):
if Binary_Search(array, targets[i], 0, N-1) == None:
print(0)
else:
print(1)
๋ง์ฐฌ๊ฐ์ง๋ก ๋ถ๋ฑํธ์ ์ฃผ์ํ์.
์ฐ์ต์ฉ ๋ฌธ์ ์ ๊ฒฝ์ฐ ์ ๋ ฅ๋ฐ์ ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฌํด์ผํ๋๋ฐ, ์์์ฝ๋์์๋ ๋งํธํ ๋ฆฌ์คํธ์ sort ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
ํต์ ๋ ฌ๊น์ง ํ๋ฒ์ ์ฐ์ตํ๊ณ ์ถ์ผ๋ฉด ํต์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํํด์ ์ฌ์ฉํด๋ ์ฐ์ต์ ์ข์๊ฒ์ด๋ค.
4. ๋ค์ต์คํธ๋ผ
https://www.acmicpc.net/problem/1753
1753๋ฒ: ์ต๋จ๊ฒฝ๋ก
์ฒซ์งธ ์ค์ ์ ์ ์ ๊ฐ์ V์ ๊ฐ์ ์ ๊ฐ์ E๊ฐ ์ฃผ์ด์ง๋ค. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000) ๋ชจ๋ ์ ์ ์๋ 1๋ถํฐ V๊น์ง ๋ฒํธ๊ฐ ๋งค๊ฒจ์ ธ ์๋ค๊ณ ๊ฐ์ ํ๋ค. ๋์งธ ์ค์๋ ์์ ์ ์ ์ ๋ฒํธ K(1 ≤ K ≤ V)๊ฐ
www.acmicpc.net
import heapq
import sys
INF = int(1e9)
def FastInput():
return sys.stdin.readline().rstrip()
node_num, vertex_num = map(int,FastInput().split())
starting_node=int(FastInput())
graph=[[] for _ in range(node_num+1)]
distance=[INF for _ in range(node_num+1)]
for i in range(vertex_num):
a,b,c=map(int,FastInput().split())
graph[a].append((b,c))
def Dijkstra(graph, starting_node, distance):
q=[]
heapq.heappush(q, (0, starting_node))
distance[starting_node]=0;
while q:
dist, now = heapq.heappop(q)
if distance[now] < dist:
continue
for i in graph[now]:
cost=dist+i[1]
if cost < distance[i[0]]:
distance[i[0]]=cost
heapq.heappush(q, (cost, i[0]))
Dijkstra(graph, starting_node, distance)
for i in range(1, node_num+1):
if distance[i]==INF:
print("INF")
else:
print(distance[i])
๋ถ๋ฑ์์์ ๋ฑํธ์ ์ฃผ์ํ์.
๋์์ ๋์ ์ฌ์ด์ ๊ฐ์ ์ด ์ฌ๋ฌ๊ฐ์ฌ๋ ์์์ ์ต์ ๊ฒฝ๋ก๋ฅผ ์ฐพ์์ฃผ๋ ๊ฑฑ์ ํ์ง๋ง์.
5. ํ๋ก์ด๋ ์์
https://www.acmicpc.net/problem/11404
11404๋ฒ: ํ๋ก์ด๋
์ฒซ์งธ ์ค์ ๋์์ ๊ฐ์ n์ด ์ฃผ์ด์ง๊ณ ๋์งธ ์ค์๋ ๋ฒ์ค์ ๊ฐ์ m์ด ์ฃผ์ด์ง๋ค. ๊ทธ๋ฆฌ๊ณ ์ ์งธ ์ค๋ถํฐ m+2์ค๊น์ง ๋ค์๊ณผ ๊ฐ์ ๋ฒ์ค์ ์ ๋ณด๊ฐ ์ฃผ์ด์ง๋ค. ๋จผ์ ์ฒ์์๋ ๊ทธ ๋ฒ์ค์ ์ถ๋ฐ ๋์์ ๋ฒํธ๊ฐ
www.acmicpc.net
import sys
def FastInput():
return sys.stdin.readline().rstrip()
INF=int(1e9)
N=int(input())
M=int(input())
cost_table=[[INF for i in range(N+1)] for j in range(N+1)]
for m in range(M):
a,b,c=map(int,FastInput().split())
if cost_table[a][b]>c:
cost_table[a][b]=c
for i in range(1, N+1):
cost_table[i][i]=0
for pivot in range(1, N+1):
for node1 in range(1, N+1):
for node2 in range(1, N+1):
cost_table[node1][node2]=min(cost_table[node1][node2], cost_table[node1][pivot]+cost_table[pivot][node2])
for i in range(1,N+1):
for j in range(1,N+1):
if cost_table[i][j]==INF:
print("%d "%(0), end="")
else:
print("%d "%cost_table[i][j], end="")
print()
์ผ์ค ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ตฌํํ๋ ํ๋ก์ด๋ ์์ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
์ผ์ค ๋ฐ๋ณต๋ฌธ์์ pivot์ด ๊ฐ์ฅ ๋ฐ๊นฅ ๋ฐ๋ณต๋ฌธ์ ๋ค์ด๊ฐ์ผ ํจ์ ์ฃผ์ํ์.
6. ์๋ก์ ์งํฉ
https://www.acmicpc.net/problem/1717
1717๋ฒ: ์งํฉ์ ํํ
์ฒซ์งธ ์ค์ n(1 ≤ n ≤ 1,000,000), m(1 ≤ m ≤ 100,000)์ด ์ฃผ์ด์ง๋ค. m์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ ์ฐ์ฐ์ ๊ฐ์์ด๋ค. ๋ค์ m๊ฐ์ ์ค์๋ ๊ฐ๊ฐ์ ์ฐ์ฐ์ด ์ฃผ์ด์ง๋ค. ํฉ์งํฉ์ 0 a b์ ํํ๋ก ์ ๋ ฅ์ด ์ฃผ์ด์ง๋ค. ์ด๋
www.acmicpc.net
import sys
sys.setrecursionlimit(100000)
def Find(parent, x):
if parent[x] != x:
parent[x]= Find(parent, parent[x])
return parent[x]
def Union(parent, a, b):
a=Find(parent,a)
b=Find(parent,b)
if a<b:
parent[b]=a
else:
parent[a]=b
def FastInput():
return sys.stdin.readline().rstrip()
N, M=map(int, FastInput().split())
parent=list(range(N+1))
for i in range(M):
operation_type, a, b= map(int,FastInput().split())
if operation_type == 0:
Union(parent, a, b)
continue
a=Find(parent, a)
b=Find(parent, b)
if a == b:
print("YES")
else:
print("NO")
๊ฒฝ๋ก์์ถ๊ธฐ๋ฒ์ ์ ๊ธฐ์ตํ๋๋กํ๊ณ ,
์ฌ๊ท์ ํ์ ๋ ํฌ๊ฒ ์ค์ ํ๋ ๊ฒ์ ์์ง ๋ง์.
7. ํฌ๋ฃจ์ค์นผ
https://www.acmicpc.net/problem/1922
1922๋ฒ: ๋คํธ์ํฌ ์ฐ๊ฒฐ
์ด ๊ฒฝ์ฐ์ 1-3, 2-3, 3-4, 4-5, 4-6์ ์ฐ๊ฒฐํ๋ฉด ์ฃผ์ด์ง output์ด ๋์ค๊ฒ ๋๋ค.
www.acmicpc.net
import sys
sys.setrecursionlimit(1000000)
def FastInput():
return sys.stdin.readline().rstrip()
def Find(parent, x):
if parent[x] != x:
parent[x] = Find(parent, parent[x])
return parent[x]
def Union(parent, a, b):
a=Find(parent, a)
b=Find(parent, b)
if a<b:
parent[b]=a
else:
parent[a]=b
N=int(FastInput())
M=int(FastInput())
parent=list(range(N+1))
edges=[]
total_cost=0
for i in range(M):
a,b,c=map(int,FastInput().split())
edges.append((c,a,b))
edges.sort()
for e in edges:
cost, a, b= e
if Find(parent, a)!=Find(parent, b):
Union(parent, a, b)
total_cost+=cost
print(total_cost)
๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ๊ณผ ์๋ก์ ์งํฉ ์๊ณ ๋ฆฌ์ฆ์ ํ์ฉํ๋ ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ ์์ ์ด๋ค.
8. ์์์ ๋ ฌ
https://www.acmicpc.net/problem/2252
2252๋ฒ: ์ค ์ธ์ฐ๊ธฐ
์ฒซ์งธ ์ค์ N(1 ≤ N ≤ 32,000), M(1 ≤ M ≤ 100,000)์ด ์ฃผ์ด์ง๋ค. M์ ํค๋ฅผ ๋น๊ตํ ํ์์ด๋ค. ๋ค์ M๊ฐ์ ์ค์๋ ํค๋ฅผ ๋น๊ตํ ๋ ํ์์ ๋ฒํธ A, B๊ฐ ์ฃผ์ด์ง๋ค. ์ด๋ ํ์ A๊ฐ ํ์ B์ ์์ ์์ผ ํ๋ค๋ ์
www.acmicpc.net
from collections import deque
import sys
sys.setrecursionlimit(1000000)
def FastInput():
return sys.stdin.readline().rstrip()
N, M = map(int,FastInput().split())
indegree=[0]*(N+1)
graph=[[] for i in range(N+1)]
for i in range(M):
a, b = map(int, FastInput().split())
graph[a].append(b)
indegree[b]+=1
def TopologySort(graph, indegree):
result = []
q = deque()
for i in range(1, N+1):
if indegree[i]==0:
q.append(i)
while q:
now = q.popleft()
result.append(now)
for i in graph[now]:
indegree[i] -= 1
if indegree[i] == 0:
q.append(i)
for i in result:
print(i, end=' ')
TopologySort(graph, indegree)