๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2024.01.08 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2024๋…„ 1์›” 8์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
8/34
post-thumbnail

1068๋ฒˆ - ํŠธ๋ฆฌ

ํŠธ๋ฆฌ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ๊ณ ๋ฅด๊ณ  ํ•ด๋‹น ๋…ธ๋“œ์˜ ํ•˜์œ„๋…ธ๋“œ๊นŒ์ง€ ์‹น ๋‹ค ์ง€์› ์„ ๋•Œ ๋ฆฌํ”„ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•ด๋‚ด๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

N = int(input())
graph = [[0]*N for _ in range(N)]
lst = list(map(int,input().split()))
erase = int(input())
visited = [0]*N

๋จผ์ € ๊ธฐ๋ณธ์ ์ธ ์ž…๋ ฅ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
lst์˜ ๊ฐ ์ธ๋ฑ์Šค๋งˆ๋‹ค ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 0๋ฒˆ ์ธ๋ฑ์Šค์— 1์ด ์žˆ๋‹ค๋ฉด ๋…ธ๋“œ 0๊ณผ ๋…ธ๋“œ 1์€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. -1์€ ๋ถ€๋ชจ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์ธ๋ฑ์Šค์˜ ๋“ค์–ด์žˆ๋Š”๋ฐ ํ•ด๋‹น ์ธ๋ฑ์Šค๋Š” ๋ฃจํŠธ ๋…ธ๋“œ๋ผ๊ณ  ๋ด๋„ ๋ฌด๋ฐฉํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

sum = 0
for values in lst:
    if values == -1:
        sum += 1
        continue
    else:
        graph[sum][values] = graph[values][sum] = 1
    sum += 1

๊ทธ๋ž˜ํ”„์˜ 2์ฐจ์›๋ฐฐ์—ด๋กœ์จ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„[์ธ๋ฑ์Šค][๋ฒจ๋ฅ˜] = ๊ทธ๋ž˜ํ”„[๋ฒจ๋ฅ˜][์ธ๋ฑ์Šค] = 1๋กœ ํ‘œ์‹œํ•˜์—ฌ ๋…ธ๋“œ๋ผ๋ฆฌ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

for i in range(len(graph)):
    for j in range(len(graph[0])):
        if i == erase:
            graph[i][j] = 0
        if j == erase:
            graph[i][j] = 0

์ €ํฌ๋Š” ๋…ธ๋“œ์™€์˜ ์—ฐ๊ฒฐ์„ ๋Š๊ธฐ ์œ„ํ•œ erase ๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ด์ œ erase์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๊ฐ€ graph์— ์žˆ๋‹ค๋ฉด ์ฃ„๋‹ค 0์œผ๋กœ ๋งŒ๋“ค์–ด ๋…ธ๋“œ ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ์„ ๋Š์–ด์ค์‹œ๋‹ค.

ans = 0
dfs(lst.index(-1))

์ด์ œ dfs๋ฅผ ๋ฃจํŠธ๋…ธ๋“œ๋ถ€ํ„ฐ ๋Œ๋ฆฌ๊ธฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
0์ด ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์–ด์„œ ๋งŒ์•ฝ 0๋ถ€ํ„ฐ ๋Œ๋ฆฌ๋ฉด ๊ฒฐ๊ณผ๊ฐ’์ด ํ‹€๋ฆฌ๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

def dfs(N):
    global ans
    visited[N] = 1
    boolean = False

N์ด ๋“ค์–ด์˜ค๋ฉด ๋ฐฉ๋ฌธํ‘œ๊ธฐ๋ฅผ ํ•ด์ฃผ๊ณ 

    for i in range(len(graph)):
        if graph[N][i] == 1 and not visited[i]:
            boolean = True
            dfs(i)

N๊ณผ ์—ฐ๊ฒฐ๋œ ์ ‘์ ์ด ์žˆ๋‹ค๋ฉด ๊ทธ ๋…ธ๋“œ์— ๊ฐ€์„œ ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•ด ๋˜ ํƒ์ƒ‰ํ•ด์ค์‹œ๋‹ค.
์—ฌ๊ธฐ์„œ ์ œ๊ฐ€ boolean๊ฐ’์„ ์ผ๋Š”๋ฐ ์ด๊ฑด ๋ฆฌํ”„๋…ธ๋“œ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด์„œ ๋‹ค ๋ฐฉ๋ฌธํ–ˆ๊ณ  ์ €๊ธฐ ์œ„์— ์ ํ˜€์žˆ๋Š” if์‹์ด ์•„์˜ˆ ๋ฐœ๋™๋˜์ง€ ์•Š์œผ๋ฉด ๊ทธ๊ฑด ํƒ์ƒ‰์ด ๋ง‰ ๋๋‚ฌ๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ boolean์€ False๊ฐ’์„ ์œ ์ง€ํ•˜๊ณ  ํ˜„์žฌ ํƒ์ƒ‰์ด ์ง„ํ–‰์ค‘์ธ ๊ฐ’์€ ๋ฆฌํ”„๋…ธ๋“œ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ

    if boolean == False:
        ans += 1

๋ฆฌํ”„๋…ธ๋“œ๊ฐ€ ๋œ๋‹ค๋ฉด ans์˜ ์นด์šดํŠธ๋ฅผ 1 ์˜ฌ๋ ค์ค์‹œ๋‹ค.

if lst.index(-1) == erase:
    print(0)
else:
    print(ans)

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ฃจํ”„ ๋…ธ๋“œ๋ฅผ ์ง€์›Œ๋ฒ„๋ฆฌ๋ฉด ๋ฆฌํ”„๋…ธ๋“œ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ 0์„ ์ถœ๋ ฅํ•˜๊ณ  (๋‹ค์ง€์›Œ์ง€๋‹ˆ๊นŒ)
์•„๋‹ˆ๋ฉด ans๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ๋‹ต์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


21937๋ฒˆ - ์ž‘์—…

์ด ๋ฌธ์ œ๋Š” ๋ฌธ์ œ ์ž์ฒด๋ณด๋‹ค ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ์™€ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋“ฑ ๊ฐ์ข… ์˜ค๋ฅ˜๋ฅผ ํ”ผํ•˜๋Š”๊ฒŒ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค...
์ผ๋‹จ ๋ฌธ์ œ๋Š” ์ž‘์—… ํ๋ฆ„์ด ์žˆ๊ณ , ๋ฆฌํ”„ ๋…ธ๋“œ๋ฅผ ์ž‘์—…ํ•˜๊ธฐ ์œ„ํ•ด์„  ๊ทธ ์ „ ์ž‘์—… ํ๋ฆ„์˜ ๋…ธ๋“œ๋ฅผ ์ž‘์—…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋…ธ๋“œ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋ช‡ ๊ฐœ์˜ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ž‘์—…ํ•ด์•ผ ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ฌป๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

ํ‘ธ๋Š” ๊ฐœ๋… ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ๋…ธ๋“œ์˜ ์—ญ์ˆœ์œผ๋กœ ๊ทธ๋ž˜ํ”„ ์ˆœ์„œ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ 
(1->6 ์ด๋ผ๋ฉด 6->1๋กœ) ๊ทธ๋ ‡๊ฒŒ ์™„์„ฑ๋œ ์—ญ์ˆœ๊ทธ๋ž˜ํ”„์—์„œ ์ฃผ์–ด์ง„ ๋…ธ๋“œ์˜ ํ•˜์œ„๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ฃผ๋ฉด ๋๋‚ฉ๋‹ˆ๋‹ค.


์ฒ˜์Œ์— ํ’€์—ˆ๋˜ ๋ฐฉ์‹

N,M = map(int,input().split())
graph = [[0]*(N+1) for _ in range(N+1)]
visited = [0]*(N+1)
for _ in range(M):
    a,b = map(int,input().split())
    graph[b][a] = 1

N๊ณผ M์„ ์ž…๋ ฅ๋ฐ›์•„์ฃผ๊ณ  graph๋ฅผ [0]์ด N+1๊ฐœ๋งŒํผ ๋“ค์–ด์žˆ๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  graph[b][a]๋ฅผ 1๋กœ ์„ ์–ธํ•จ์œผ๋กœ์จ b->a์ธ ์—ญ์ˆœ๊ทธ๋ž˜ํ”„๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

work = int(input())
sum = 0
dfs(work)

์ฃผ์–ด์ง„ ๋…ธ๋“œ๋ฅผ ๋ฐ›์•„์ฃผ๊ณ  ํ•ด๋‹น ๋…ธ๋“œ์˜ ํ•˜์œ„๋…ธ๋“œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ dfs๋ฅผ ๋Œ๋ฆฝ๋‹ˆ๋‹ค.

def dfs(N):
    global sum
    visited[N] = 1
    for i in range(len(graph)):
        if graph[N][i] == 1 and not visited[i]:
            sum += 1
            dfs(i)

๋ฐฉ๋ฌธํ‘œ๊ธฐ๋ฅผ ํ•ด์ค€ ํ›„ ๊ทธ๋ž˜ํ”„ ๊ธธ์ด๋งŒํผ ๋Œ๋ฆฌ๊ณ  ๋งŒ์•ฝ ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๊ฒŒ ์žˆ๊ณ  ๋ฐฉ๋ฌธ์„ ์•ˆํ–ˆ์œผ๋ฉด sum๊ฐ’์„ 1 ๋†’์—ฌ์ฃผ๊ณ  ํ•˜์œ„๋…ธ๋“œ๋กœ ๋‹ค์‹œ ์žฌ๊ท€๋ฅผ ๋Œ๋ฆฝ์‹œ๋‹ค.

print(sum)

๊ทธ๋ฆฌ๊ณ  ์ถœ๋ ฅํ•˜๋ฉด

๋‹ต์€ ์ •์ƒ์ ์œผ๋กœ ๋‚˜์˜ค๋Š”๋ฐ

๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค!
์ด๊ฑธ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ƒ๊ฐ์„ ๊ฑฐ๋“ญํ•ด๋ดค์Šต๋‹ˆ๋‹ค.


๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ํ•ด๊ฒฐ

์—ฌ๊ธฐ์„œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด๋ž€ ์žฌ๊ท€์˜ ํฌ๊ธฐ์— ์ค‘์ ์„ ์žก๊ธฐ๋ณด๋‹ค๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ ์ผ์–ด๋‚œ ์ผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์งˆ๋ฌธ๊ธ€์„ ์˜ฌ๋ ค๋ณด๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ๊ฐ€ 10GB๊ฐ€ ๋„˜์„๊ฒƒ ๊ฐ™๋‹ค๊ณ  ํ•˜์‹œ๋„ค์š”...
๋”ฐ๋ผ์„œ ๋ฐฐ์—ด์„ ์ตœ๋Œ€ํ•œ ์ ๊ฒŒ ํ™œ์šฉํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งœ๋ดค์Šต๋‹ˆ๋‹ค.

N,M = map(int,input().split())
graph = [[] for _ in range(N+1)]
visited = [0]*(N+1)
for _ in range(M):
    a,b = map(int,input().split())
    graph[b].append(a)

์ผ๋‹จ ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ์˜ ์ด์ฐจ์› ์š”์†Œ๋ฅผ ํ•˜๋‚˜๋กœ ๊ณ ์ •์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.
[[0,0,0,0,0,0],[0,0,0,0,0,0,],....] ์ด์—ˆ๋˜ ๊ฑธ [[],[]] ๋กœ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ ์„  ์ด์–ด์ง„ ๊ฑธ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์—ญ์ˆœ ๊ทธ๋ž˜ํ”„์˜ ๋์  ๋…ธ๋“œ๋ฅผ ์ธ๋ฑ์Šค, ์—ญ์ˆœ ๊ทธ๋ž˜ํ”„์˜ ์‹œ์ž‘์  ๋…ธ๋“œ๋ฅผ value๊ฐ’์œผ๋กœ ์žก์•„ ํ•ด๋‹น ๋…ธ๋“œ๋“ค์ด ์ด์–ด์กŒ๋‹ค๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

work = int(input())
sum = 0
dfs(work)

๊ทธ ์™ธ ๊ณผ์ •์€ ๋น„์Šทํ•˜๋ฉฐ

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

์ด๋”ฐ ํ•ด๋ณด๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊นŒ์ง€ ๋‚˜์„œ sysinputํ•˜๊ณ  recursionlimit ๊นŒ์ง€ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

def dfs(N):
    global sum
    visited[N] = 1
    for i in graph[N]:
        if not visited[i]:
            sum += 1
            dfs(i)

์—ฌ๊ธฐ๋„ ์ด์ „์ฝ”๋“œ์™€ ์กฐ๊ธˆ ๋‹ค๋ฅธ๋ฐ, ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•ด์„œ (๋‘˜์ด ์ด์–ด์ง„ ๋…ธ๋“œ๋‹ˆ๊นŒ) ๋งŒ์•ฝ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์•„์ง ๋ฐฉ๋ฌธ์„ ์•ˆํ•œ ์ƒํƒœ๋ผ๋ฉด ์žฌ๊ท€๋ฅผ ๋Œ๋ ค ๋ฐฉ๋ฌธํ•ด์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

print(sum)

์ถœ๋ ฅ์€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ํ•ญ์ƒ BFS๋‚˜ DFS์“ธ ๋•Œ ๋…ธ๋“œ๋“ค์ด ์ด์–ด์กŒ๋‹ค๋Š” ๊ฑธ ์ €๋Ÿฐ์”ฉ์œผ๋กœ ํ‘œํ˜„ํ•˜๋‹ค ๋ณด๋‹ˆ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ๊ฐ™์Šต๋‹ˆ๋‹ค.
์•ž์œผ๋กœ ์ด์ฐจ์› ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ๊นŒ์ง€ ๊ณ ๋ คํ•ด์„œ ์ข€ ๋” ๋ณ€์ˆ˜๋“ค์„ ํšจ์œจ์ ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ์„ ์–ธํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.




profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

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