๐Ÿ“Algorithm & Codingtest/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

๊ธฐ๋ณธ๊ธฐ ๋ฌธ์ œ ๋ฆฌ์ŠคํŠธ

Hush 2022. 6. 27. 17:13

๊ธฐ๋ณธ๊ธฐ๋ฅผ ๋‹ค์ง€๊ธฐ ์œ„ํ•œ ๋“œ๋ฆด ๋ฃจํ‹ด์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ๋“ค์ด๋‹ค.

์ด 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)