๐Ÿ“๋ฌธ์ œ ํ’€์ด/๐ŸงฉBaekjoon

    [Python] ๋ฐฑ์ค€ 1003 ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜ | DP

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1003 1003๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค 0์ด ์ถœ๋ ฅ๋˜๋Š” ํšŸ์ˆ˜์™€ 1์ด ์ถœ๋ ฅ๋˜๋Š” ํšŸ์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - DP - ์ž…๋ ฅ๊ฐ’(1~40) ์— ๋”ฐ๋ผ 0๊ณผ 1์ด ๋ถˆ๋ฆฐ ํšŸ์ˆ˜๋ฅผ ์ ํ™”์‹์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค - ์ž…๋ ฅ๊ฐ’ 0์€ 0์ด 1ํšŒ/1์ด 0ํšŒ ๋ถˆ๋ฆฐ๋‹ค. ์ž…๋ ฅ๊ฐ’ 1์€ 0์ด 0ํšŒ/1์ด 1ํšŒ ๋ถˆ๋ฆฐ๋‹ค. - ์ž…๋ ฅ๊ฐ’์ด 2๋ผ๋ฉด f(2) = f(1) + f(0) ์ด๋ฏ€๋กœ 1์ด ํ•œ ๋ฒˆ, 0์ด ํ•œ ๋ฒˆ ๋ถˆ๋ฆฐ๋‹ค. - ์ž…๋ ฅ๊ฐ’์ด 3์ด๋ผ๋ฉด f(3) = f(2) + f(1) ์ด๋ฏ€๋กœ f(2) ์‹คํ–‰์‹œ 1ํ•œ ๋ฒˆ, 0 ํ•œ ๋ฒˆ ๋ถˆ๋ฆฐ ๊ฒƒ๊ณผ 1์ด ํ•œ ๋ฒˆ ๋ถˆ๋ฆฐ ๊ฒƒ์„ ๋”ํ•˜๋ฉด 1์ด 2๋ฒˆ..

    [Python] ๋ฐฑ์ค€ 9095 1, 2, 3 ๋”ํ•˜๊ธฐ | DP

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/9095 9095๋ฒˆ: 1, 2, 3 ๋”ํ•˜๊ธฐ ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค, n์„ 1, 2, 3์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - DP(๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ)์˜ ์ „ํ˜•์ ์ธ ๋ฌธ์ œ์ด๋‹ค - DP๋Š” ํ•œ๋ฒˆ ์ง„ํ–‰ํ•œ ๊ณ„์‚ฐ์— ๋Œ€ํ•ด์„œ ์ €์žฅ ํ•ด๋‘๊ณ  ๋˜ ๋ฐœ์ƒ์‹œ ๊ณ„์‚ฐ ๊ฐ’๋งŒ ์‚ฌ์šฉํ•œ๋‹ค. - ํ•ด๋‹น ์ฝ”๋“œ์—์„œ numbers ๋ฐฐ์—ด์€ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ €์žฅํ•ด๋‘๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. - 1, 2, 3์„ ์ด์šฉํ•˜์—ฌ 1, 2, 3์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด์ž 1 = 1 1์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ 1๊ฐœ(numbers[1] = 1) 2 = 1 + 1 2 = 2 2๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐœ(numbers[2] = 2) 3 ..

    [Python] ๋ฐฑ์ค€ 1715 ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ | ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1715 1715๋ฒˆ: ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ ์ •๋ ฌ๋œ ๋‘ ๋ฌถ์Œ์˜ ์ˆซ์ž ์นด๋“œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž. ๊ฐ ๋ฌถ์Œ์˜ ์นด๋“œ์˜ ์ˆ˜๋ฅผ A, B๋ผ ํ•˜๋ฉด ๋ณดํ†ต ๋‘ ๋ฌถ์Œ์„ ํ•ฉ์ณ์„œ ํ•˜๋‚˜๋กœ ๋งŒ๋“œ๋Š” ๋ฐ์—๋Š” A+B ๋ฒˆ์˜ ๋น„๊ต๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผํ…Œ๋ฉด, 20์žฅ์˜ ์ˆซ์ž ์นด๋“œ ๋ฌถ์Œ๊ณผ 30์žฅ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ์ž‘์€ ์นด๋“œ ๋ญ‰์น˜๋“ค ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค! - ์นด๋“œ ๋ญ‰์น˜๊ฐ€ 1๊ฐœ๋ผ๋ฉด ๋น„๊ตํ•  ๋ญ‰์น˜๊ฐ€ ์—†์œผ๋ฏ€๋กœ 0 ๋ฐ˜ํ™˜ - ์นด๋“œ ๋ญ‰์น˜๊ฐ€ 3๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ์ตœ์†Œ๊ฐ’ 2๊ฐœ๋ฅผ ๋ฝ‘์•„ ์ˆœ์„œ๋Œ€๋กœ ์—ฐ์‚ฐ(์šฐ์„ ์ˆœ์œ„ ํ) - ํ ์•ˆ์— ๋ญ‰์น˜๊ฐ€ 2๊ฐœ ๋‚จ์•˜๋‹ค๋ฉด ๋”์ด์ƒ ์ตœ์†Œ ๊ฐ’ 2๊ฐœ๋ฅผ ์ฐพ์ง€ ์•Š๊ณ  ์—ฐ์‚ฐ ๊ฐ’์„ ๋”ํ•˜์—ฌ ์ถœ๋ ฅ & ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฃŒ..

    [Python] ๋ฐฑ์ค€ 1774 ์šฐ์ฃผ์‹ ๊ณผ์˜ ๊ต๊ฐ | ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1774 21924๋ฒˆ: ๋„์‹œ ๊ฑด์„ค ์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ๊ฐœ์ˆ˜ $N$ $(3 \le N \le 10^5 )$์™€ ๋„๋กœ์˜ ๊ฐœ์ˆ˜ $M$ $(2 \le M \le min( {N(N-1) \over 2}, 5×10^5)) $๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„ ๋ถ€ํ„ฐ $M + 1$์ค„๊นŒ์ง€ ๊ฑด๋ฌผ์˜ ๋ฒˆํ˜ธ $a$, $b$ $(1 \le a, b \le N, a ≠ b)$์™€ ๋‘ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - dots : ๊ฐ ์ (์ •์ , ๋…ธ๋“œ) ๋ณ„ x, y ์ขŒํ‘œ ์ €์žฅ - edges : ๊ฐ„์„ ์˜ ๋น„์šฉ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ - parent : ์ •์  ๋ณ„ ์†ํ•œ ์ง‘ํ•ฉ ์ •๋ณด - ๊ฐ ์ •์  ์ •๋ณด๋ฅผ ์ˆœ์„œ๋Œ€๋กœ dots..

    [Python] ๋ฐฑ์ค€ 21924 ๋„์‹œ ๊ฑด์„ค | ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/21924 21924๋ฒˆ: ๋„์‹œ ๊ฑด์„ค ์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ๊ฐœ์ˆ˜ $N$ $(3 \le N \le 10^5 )$์™€ ๋„๋กœ์˜ ๊ฐœ์ˆ˜ $M$ $(2 \le M \le min( {N(N-1) \over 2}, 5×10^5)) $๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„ ๋ถ€ํ„ฐ $M + 1$์ค„๊นŒ์ง€ ๊ฑด๋ฌผ์˜ ๋ฒˆํ˜ธ $a$, $b$ $(1 \le a, b \le N, a ≠ b)$์™€ ๋‘ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž‘์„ฑ ์ฝ”๋“œ import sys input = sys.stdin.readline N, M = map(int, input().split()) parent = [i for i in range(N..

    [Python] ๋ฐฑ์ค€ 19368 ํ–‰์„ฑ ์—ฐ๊ฒฐ | ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/19368 14621๋ฒˆ: ๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์•  ์ž…๋ ฅ์˜ ์ฒซ์งธ ์ค„์— ํ•™๊ต์˜ ์ˆ˜ N์™€ ํ•™๊ต๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 1,000) (1 ≤ M ≤ 10,000) ๋‘˜์งธ ์ค„์— ๊ฐ ํ•™๊ต๊ฐ€ ๋‚จ์ดˆ ๋Œ€ํ•™๊ต๋ผ๋ฉด M, ์—ฌ์ดˆ ๋Œ€ํ•™๊ต๋ผ๋ฉด W์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž‘์„ฑ ์ฝ”๋“œ import sys input = sys.stdin.readline n = int(input()) edges = [] # ๋น„์šฉ ์˜ค๋ฆ„์ฐจ์ˆœ์„ ์œ„ํ•œ ๋ฆฌ์ŠคํŠธ parent = [i for i in range(n+1)] # ์„œ๋กœ์†Œ ์ง‘ํ•ฉ (๋ณธ์ธ์„ ๋ถ€๋ชจ๋กœ ๊ฐ–๋„๋ก ์ดˆ๊ธฐํ™”) f..

    [Python] ๋ฐฑ์ค€ 14621 ๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์•  | ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/14621 14621๋ฒˆ: ๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์•  ์ž…๋ ฅ์˜ ์ฒซ์งธ ์ค„์— ํ•™๊ต์˜ ์ˆ˜ N์™€ ํ•™๊ต๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 1,000) (1 ≤ M ≤ 10,000) ๋‘˜์งธ ์ค„์— ๊ฐ ํ•™๊ต๊ฐ€ ๋‚จ์ดˆ ๋Œ€ํ•™๊ต๋ผ๋ฉด M, ์—ฌ์ดˆ ๋Œ€ํ•™๊ต๋ผ๋ฉด W์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ - school : ์ •์  ๋ณ„ ํ•™๊ต ์„ฑ๋ณ„ - parent : ์ •์  ๋ณ„ ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์ •๋ณด - edges : ๊ฐ„์„ ์˜ ๋น„์šฉ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ณด ์ €์žฅ - ์ •๋ ฌ๋œ edges๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉฐ ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ์—ฐ๊ฒฐ - ์„œ๋กœ์†Œ ์ง‘ํ•ฉ์ด ์ •์  ๋ณธ์ธ๊ณผ ๊ฐ™์€ ์ •์ ์˜ ๊ฐœ์ˆ˜..

    [Python] ๋ฐฑ์ค€ 6497 ์ „๋ ฅ๋‚œ | ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/6497 6497๋ฒˆ: ์ „๋ ฅ๋‚œ ์„ฑ์ง„์ด๋Š” ํ•œ ๋„์‹œ์˜ ์‹œ์žฅ์ธ๋ฐ ๊ฑฐ์ง€๋ผ์„œ ์ „๋ ฅ๋‚œ์— ๋™๋™๋Œ„๋‹ค. ๊ทธ๋ž˜์„œ ๋ชจ๋“  ๊ธธ๋งˆ๋‹ค ์›๋ž˜ ์ผœ์ ธ ์žˆ๋˜ ๊ฐ€๋กœ๋“ฑ ์ค‘ ์ผ๋ถ€๋ฅผ ์†Œ๋“ฑํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๊ธธ์˜ ๊ฐ€๋กœ๋“ฑ์„ ์ผœ ๋‘๋ฉด ํ•˜๋ฃจ์— ๊ธธ์˜ ๋ฏธํ„ฐ ์ˆ˜๋งŒํผ ๋ˆ์ด ๋“ค www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ (์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ) ์•Œ๊ณ ๋ฆฌ์ฆ˜ - find ํ•จ์ˆ˜ : ๋ถ€๋ชจ ๋…ธ๋“œ(์–ด๋Š ์ง‘ํ•ฉ์— ์†ํ•˜๋Š”์ง€) ํ™•์ธ - union ํ•จ์ˆ˜ : ์ •์  a, b๋ฅผ ๊ฐ™์€ ์ง‘ํ•ฉ์— ์†ํ•˜๋„๋ก parent ์—…๋ฐ์ดํŠธ - ์—ฌ๋Ÿฌ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์žˆ์œผ๋ฏ€๋กœ n, m์ด 0, 0 ์ผ๋•Œ ์ข…๋ฃŒ ์กฐ๊ฑด์œผ๋กœ ๋ฌดํ•œ ๋ฐ˜๋ณต - edges : ๊ฐ„์„ ์„ ๋น„์šฉ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅ - parent : ๊ฐ ..

    [Python] ๋ฐฑ์ค€ 4386 ๋ณ„์ž๋ฆฌ ๋งŒ๋“ค๊ธฐ | ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/4386 4386๋ฒˆ: ๋ณ„์ž๋ฆฌ ๋งŒ๋“ค๊ธฐ ๋„ํ˜„์ด๋Š” ์šฐ์ฃผ์˜ ์‹ ์ด๋‹ค. ์ด์ œ ๋„ํ˜„์ด๋Š” ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๋„๋ธŒ๋Ÿฌ์ ธ ์žˆ๋Š” n๊ฐœ์˜ ๋ณ„๋“ค์„ ์ด์–ด์„œ ๋ณ„์ž๋ฆฌ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค ๊ฒƒ์ด๋‹ค. ๋ณ„์ž๋ฆฌ์˜ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ๋ณ„์ž๋ฆฌ๋ฅผ ์ด๋ฃจ๋Š” ์„ ์€ ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๋ณ„์„ ์ผ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ (์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ) ์•Œ๊ณ ๋ฆฌ์ฆ˜ - alist : ๊ฐ ์ •์ ์˜ x, y ์ •๋ณด ์ €์žฅ - edges : ์ •์  ๊ฐ„ ๋ชจ๋“  ๊ฐ„์„  ๊ธธ์ด( sqrt((x1 - x2)**2 + (y1 - y2)**2) ) ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅ - parent : ์ง‘ํ•ฉ์ •๋ณด ( ์—ฐ๊ฒฐ๋œ ์ •์  ์ •๋ณด) - find ํ•จ์ˆ˜ : ๊ฐ™์€ ์ง‘ํ•ฉ์— ์†ํ•ด์žˆ๋Š”์ง€ ํ™•์ธ - unio..