TIL-2024/04/03

๋ฐ•์ƒ์šฐยท2024๋…„ 8์›” 12์ผ
0

๐Ÿ“ TIL

๋ชฉ๋ก ๋ณด๊ธฐ
12/45
post-thumbnail

๋ฐฑ์ค€ - 1707๋ฒˆ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„

import sys
from collections import deque
sys.setrecursionlimit(10**6)

input = sys.stdin.readline

# DFS๋กœ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ฐ™์€ ๊ทธ๋ฃน์˜ ๋…ธ๋“œ๋ฅผ ๋งŒ๋‚˜๋ฉด ํƒ์ƒ‰์„ ๋๋‚ด๊ณ  False๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
def dfs(start, flag, graph, group):
	#  ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ทธ๋ฃนํ™”ํ•œ๋‹ค.
  flag[start] = group
	
	# ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ์–ด์žˆ์œผ๋ฉด์„œ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ๋“ค์„ ์ˆœํšŒํ•˜๋ฉฐ ๊นŠ์ด ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  for e in graph[start]:
    if flag[e] == 0:
      res = dfs(e, flag, graph, -group)
      
      if not res:
        return False
        
   # ๊ฐ™์€ ๊ทธ๋ฃน์˜ ๋…ธ๋“œ๊ฐ€ ์ด์›ƒํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
    elif flag[e] == group:
      return False
  
  return True

K = int(input())

for n in range(K):
  v, edges = map(int, input().split())
  graph = [[ ] for i in range(v+1)]
  visited = [0]*(v+1)
  e_list = []
	
	# ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  for _ in range(edges):
      x, y = map(int, input().split())
      graph[x].append(y)
      graph[y].append(x)
      e_list.append([x, y])

	# ๋…ธ๋“œ๋“ค์ด ๋Š์–ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.
  for index in range(1, v + 1):
	  # ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
    if visited[index] == 0:
		   # ๊นŠ์ด ํƒ์ƒ‰์„ ํ†ตํ•ด ์ด์–ด์ง„ ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๋…ธ๋“œ๋ฅผ ๊ทธ๋ฃนํ™”ํ•œ๋‹ค.
      res = dfs(index, visited, graph, 1)
      
      # ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์—์„œ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฉด ๋ฐ˜๋ณต์„ ์ค‘๋‹จํ•œ๋‹ค.
      if not res:
        break

  print('YES') if res else print('NO')
    

๐Ÿ˜ฉย ๋ชป ํ‘ผ ์›์ธ ๐Ÿ˜ฉ

๋ฌธ์ œ ์ดํ•ด๋ฅผ ์ œ๋Œ€๋กœ ๋ชปํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ๋ฌธ์ œ ๊ทธ๋Œ€๋กœ ์ดํ•ดํ–ˆ์œผ๋ฉด ์ •์ ์„ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐฉ์‹์„ ๊ณ ๋ฏผํ–ˆ์„ํ…๋ฐ ๊ฐ„์„ ์„ ์ œ๊ฑฐํ–ˆ์„ ๋•Œ ์™„์ „ํžˆ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด์•ผํ•œ๋‹ค๋Š” ๋‰˜์•™์Šค๋กœ ์ž˜๋ชป ์ดํ•ดํ•ด์„œ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐฑ์ค€ - 2665๋ฒˆ ๋ฏธ๋กœ ๋งŒ๋“ค๊ธฐ

import sys
import heapq

input = sys.stdin.readline

N = int(input())

maze = [ list(map(int, input()[:-1])) for _ in range(N) ]
visited = [ [ False ] * N for _ in range(N) ]

# ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
queue = [[0, 0, 0]]

dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]

while queue:
  cost, x, y = heapq.heappop(queue)
  # ๋ฏธ๋กœ ๋์— ๋„์ฐฉํ•œ ๊ฒฝ์šฐ ๋ฐ˜๋ณต์„ ๋๋‚ด๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  if x == N - 1 and y == N - 1:
    print(cost)
    break
	
	# ํ•ด๋‹น ์นธ์— ๋Œ€ํ•ด์„œ ์ƒ, ํ•˜, ์ขŒ, ์šฐ ์นธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
  for i in range(4):
    nx = x + dx[i]
    ny = y + dy[i]
    # ๋ฏธ๋กœ๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์นธ๋งŒ ํ•ธ๋“ค๋งํ•œ๋‹ค.
    if 0 <= nx < N and 0 <= ny < N and visited[ny][nx] == False:
      visited[ny][nx] = True
      
      # ๋‹ค์Œ ์นธ์ด ๊ฒ€์€๋ฐฉ์ธ ๊ฒฝ์šฐ cost ๊ฐ’ ๊ฐฑ์‹ 
      if maze[ny][nx] == 1:
        heapq.heappush(queue, [cost, nx, ny])
      else:
        heapq.heappush(queue, [cost + 1, nx, ny])
        
# ** ์ ‘๊ทผ ๋ฐฉ์‹ **
# ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•œ BFS๋ฅผ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด๋œ๋‹ค.
# ํŠนํžˆ ์šฐ์„  ์ˆœ์œ„ ํ์˜ ๋™์ž‘์— ์ง‘์ค‘ํ•ด์„œ ์–ด๋–ค ๋ฐฉ์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๋Š”์ง€ ์ƒ๊ฐํ•ด๋ณผ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.
# ํ•˜์–€๋ฐฉ์ด๋˜, ๊ฒ€์€ ๋ฐฉ์ด๋˜ ๋ฐฉ์„ ๋งŒ๋‚ฌ์„ ๋•Œ ์šฐ์„  ์ˆœ์œ„ ํ์— ์ถ”๊ฐ€๋˜๋Š” ๋ถ€๋ถ„์€ ๊ฐ™์ง€๋งŒ,
# ๊ฒ€์€ ๋ฐฉ์˜ ๊ฒฝ์šฐ cost๊ฐ€ ๋Š˜์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„  ์ˆœ์œ„ ํ ๋‚ด๋ถ€์—์„œ ์ˆœ์„œ๊ฐ€ ๋’ค๋กœ ๋ฐ€๋ฆฌ๊ฒŒ ๋œ๋‹ค.
# ๊ทธ๋ž˜์„œ ๊ฐ™์€ cost์˜ ํ•˜์–€ ๋ฐฉ์„ BFS๋กœ ํƒ์ƒ‰ํ•˜๊ณ , ๋ฏธ๋กœ ๋์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ,
# ๋‹ค์Œ ๋ฐฉ์„ ์—ด์–ด ํƒ์ƒ‰์„ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋œ๋‹ค.

๐Ÿ˜ฉย ๋ชป ํ‘ผ ์›์ธ ๐Ÿ˜ฉ

BFS๋กœ ์ธ์ ‘ํ•œ ๋ฐฉ์„ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ์ด์ „ ๋ฐฉ์˜ ํšŸ์ˆ˜์™€ ํ˜„์žฌ ๋ฐฉ์ƒ‰๊น”์„ ํ†ตํ•ด์„œ ํ˜„์žฌ ๋ฐฉ์— ๋„๋‹ฌํ•˜๊ธฐ๊นŒ์ง€ ๋” ์ ๊ฒŒ ๋ฌธ์„ ์—ฐ ํšŸ์ˆ˜๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œํ•ด๊ฒฐ์„ ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๋ฐฉ์€ ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ๋ฅผ ํ†ตํ•ด ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • ๋ฐฉ์„ ์ด๋ฏธ ์ง€๋‚œ ์ดํ›„ ๋” ์ ์€ ํšŸ์ˆ˜๋กœ ๋‹ค์‹œ ๋„์ฐฉํ–ˆ์„ ๋•Œ ์ดํ›„ ๋ฐฉ์— ๋Œ€ํ•ด์„œ ๋˜ ๋‹ค์‹œ BFS๋ฅผ ๊ฑธ์–ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์ฐพ์ง€๋ชปํ–ˆ๋‹ค.

๋ฐฑ์ค€ 21606 - ์•„์นจ ์‚ฐ์ฑ…

60์ ์งœ๋ฆฌ ๋‚ด ์ ‘๊ทผ๋ฒ•

ํ…์ŠคํŠธ ๊ทธ๋Œ€๋กœ โ€˜์‹ค๋‚ด์—์„œ ์‹ค๋‚ด๋กœ ๋„์ฐฉโ€™ํ•˜๋Š” ์ผ€์ด์Šค๋ฅผ DFS๋กœ ๋ชจ๋“  ์‹ค๋‚ด ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ countํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ–ˆ์„ ๋•Œ ๊ฒฐ๊ณผ๋Š” ๋ถ€๋ถ„ ์ •๋‹ต๋ฐ–์— ๋ฐ›์„ ์ˆ˜ ์—†์—ˆ๊ณ , ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.

import sys

input = sys.stdin.readline

N = int(input())

A = [0] + [ int(i) for i in input()[:-1] ]

graph = [ [] for _ in range(N + 1) ]

for _ in range(N - 1):
  f, t = map(int, input().split())
  graph[f].append(t)
  graph[t].append(f)

count = 0

def dfs(count, start, isOut):
  visited[start] = True
  # ์ด์ œ ๋ง‰ ๋‚˜์˜จ ์ผ€์ด์Šค
  if isOut == False and A[start] == 1:
    isOut = True
  # ์‹ค์™ธ์— ์žˆ๋‹ค๊ฐ€ ์‹ค๋‚ด๋กœ ๋“ค์–ด์˜จ๊ฑด์ง€
  elif isOut == True and A[start] == 1:
    count += 1
    return count

  for e in graph[start]:
    if visited[e] == False:
      count = dfs(count, e, isOut)

  return count

for n in range(1, N + 1):
  visited = [ False ] * ( N + 1 )
  if A[n] == 1:
    res = dfs(0, n, False)
    count += res

print(count)

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ๊ฐ„๋žตํ•˜๊ฒŒ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

๊ฒฐ๊ตญ (์‹ค๋‚ด) - (์‹ค๋‚ด) ์‚ฌ์ด์— ์‹ค์™ธ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์žˆ๋˜ ๋‘ ๊ฐœ์˜ ์‹ค๋‚ด๊ตฌ์—ญ๋งŒ ๊ณ ๋ฅด๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๋ฌธ์ œ์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

59_แ„‡แ…กแ†จแ„‰แ…กแ†ผแ„‹แ…ฎ_week1.jpeg

CASE 1 - ์‹ค์™ธ๋ฅผ ๊ฑฐ์น˜๋Š” ๊ฒฝ์šฐ

(์‹ค๋‚ด) - (์‹ค์™ธ) * x - (์‹ค๋‚ด)์˜ ๊ฒฝ์šฐ, ์‹ค์™ธ๋ฅผ ๋‘˜๋Ÿฌ์‹ธ๋Š” ๋…ธ๋“œ ์ค‘ 2๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๋‹ค.

๋”ฐ๋ผ์„œ ์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” N(N-1)์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ๋ฌธ์ œ์˜ ์˜ˆ์‹œ์—์„œ๋Š” 1, 3, 4 ๋…ธ๋“œ ์ค‘ 2๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๊ฒฝ์šฐ์ด๋ฏ€๋กœ 3 * 2๋กœ 6๊ฐœ์˜ ์ผ€์ด์Šค๊ฐ€ ๋‚˜์˜จ๋‹ค.

59_แ„‡แ…กแ†จแ„‰แ…กแ†ผแ„‹แ…ฎ_week1 2.jpeg

์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์‹ค์™ธ ์‚ฌ์ด์— ์‹ค๋‚ด๊ฐ€ ๋ผ์–ด์žˆ๋Š” ํ˜•ํƒœ์˜ ๊ฒฝ์šฐ์—๋„ ๋™์ผํ•˜๋‹ค.

ํ•˜์ง€๋งŒ ์•ฝ๊ฐ„์˜ ์„ค๊ณ„๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

์‹ค์™ธ ๋…ธ๋“œ์—์„œ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๊ณ  ์‹ค๋‚ด๋ฅผ ๋งŒ๋‚˜๋ฉด ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•˜๊ฒŒ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฐ˜๋Œ€ํŽธ ์‹ค์™ธ์— ๋Œ€ํ•œ ํƒ์ƒ‰์ด ๋Š๊ธฐ๋Š” ์ƒํƒœ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด์„œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ตœ์†Œํ•œ ํ•œ๋ฒˆ์€ ํƒ์ƒ‰ํ•˜๋„๋ก ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

CASE 2 - ์‹ค์™ธ๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

์‹ค์™ธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ๋ฒˆ์งธ ์ผ€์ด์Šค์™€๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํšŸ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

์ธ์ ‘ํ•œ ์‹ค๋‚ด๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒฝ์šฐ์— (์‹ค๋‚ด1) โ†’ (์‹ค๋‚ด2), (์‹ค๋‚ด2) โ†’ (์‹ค๋‚ด1) ์ด ๋‘๋ฒˆ์„ ์ด ํšŸ์ˆ˜์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

โš™๏ธย ์ˆ˜์ • ์ฝ”๋“œ โš™๏ธ

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def dfs(v):
  visited[v] = True
  
  in_count = 0
  for e in graph[v]:
    if  A[e] == 1:
      in_count += 1
    elif not visited[e] and A[e] == 0:
      in_count += dfs(e)

  return in_count

N = int(input())
A = [0] + [ int(i) for i in input()[:-1] ]
graph = [ [] for _ in range(N + 1) ]
visited = [ False ] * ( N + 1 )
total = 0

for _ in range(N - 1):
  f, t = map(int, input().split())
  graph[f].append(t)
  graph[t].append(f)
  if A[f] == 1 and A[t] == 1: # CASE 2
    total += 2

for n in range(1, N + 1):
  if A[n] == 0 and visited[n] == False: # CASE 1
      in_count = dfs(n)
      total += in_count * (in_count - 1)

print(total)
profile
๋‚˜๋„ ์ž˜ํ•˜๊ณ  ์‹ถ๋‹ค..!

0๊ฐœ์˜ ๋Œ“๊ธ€