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

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

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1922 1922๋ฒˆ: ๋„คํŠธ์›Œํฌ ์—ฐ๊ฒฐ ์ด ๊ฒฝ์šฐ์— 1-3, 2-3, 3-4, 4-5, 4-6์„ ์—ฐ๊ฒฐํ•˜๋ฉด ์ฃผ์–ด์ง„ output์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜) - ์„œ๋กœ์†Œ ์ง‘ํ•ฉ์„ ์œ„ํ•œ find_parent, union_parent ํ•จ์ˆ˜ - edges : ๊ฐ„์„  ๋น„์šฉ, ๊ฐ„์„ a, ๊ฐ„์„ b๋ฅผ ์ €์žฅํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅ - parent : ๊ฐ ์ •์  ๋ณ„ ์ง‘ํ•ฉ ์ •๋ณด ์ €์žฅ - ์ •๋ ฌ๋œ edges ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ์‚ฌ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ฐ„์„  ์—ฐ๊ฒฐ - find_parent(a) != find_parent(b) ์ฆ‰, ๊ฐ™์€ ์ง‘ํ•ฉ์— ์†ํ•ด์žˆ์ง€ ์•Š๋‹ค ..

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

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1647 1647๋ฒˆ: ๋„์‹œ ๋ถ„ํ•  ๊ณ„ํš ์ฒซ์งธ ์ค„์— ์ง‘์˜ ๊ฐœ์ˆ˜ N, ๊ธธ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 2์ด์ƒ 100,000์ดํ•˜์ธ ์ •์ˆ˜์ด๊ณ , M์€ 1์ด์ƒ 1,000,000์ดํ•˜์ธ ์ •์ˆ˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ M์ค„์— ๊ฑธ์ณ ๊ธธ์˜ ์ •๋ณด๊ฐ€ A B C ์„ธ ๊ฐœ์˜ ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง€๋Š”๋ฐ A๋ฒˆ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ๋ฌธ์ œ (ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜) - edges : ๊ฐ„์„  ๋น„์šฉ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ - parent : ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์ •๋ณด ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ - find_parent(x) : ์„œ๋กœ ๊ฐ™์€ ์ง‘ํ•ฉ์— ์†ํ•ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜ - union_parent(a, b) : a, b ์ •์ ์— ๋Œ€ํ•ด..

    [Python] ๋ฐฑ์ค€ 1197 ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1197 1197๋ฒˆ: ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V(1 ≤ V ≤ 10,000)์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E(1 ≤ E ≤ 100,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ E๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” A๋ฒˆ ์ •์ ๊ณผ B๋ฒˆ ์ •์ ์ด www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋ฌธ์ œ ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. - parent : ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ๊ฐœ๋…์—์„œ ๊ฐ ์ •์ ์ด ์–ด๋Š ์ง‘ํ•ฉ์— ์†ํ•ด์žˆ๋Š”์ง€ ์ •๋ณด๋ฅผ ์ €์žฅ - edges : ๊ฐ„์„ ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„. ๊ฐ€์žฅ ์ž‘์€ ๋น„์šฉ์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฐ„์„ ๋ถ€ํ„ฐ ์—ฐ๊ฒฐํ•ด๋‚˜๊ฐ„๋‹ค - find_parent : ํ•ด๋‹น..

    [Python] ๋ฐฑ์ค€ 18405 ๊ฒฝ์Ÿ์  ์ „์—ผ | BFS

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/18405 18405๋ฒˆ: ๊ฒฝ์Ÿ์  ์ „์—ผ ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N, K๊ฐ€ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 200, 1 ≤ K ≤ 1,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์‹œํ—˜๊ด€์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ–‰์€ N๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ํ•ด๋‹น ์œ„์น˜ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํŠน์ • ์œ„์น˜๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ๋ป—์–ด๊ฐ€๋ฏ€๋กœ BFS๊ฐ€ ์ ์ ˆ - ์ˆซ์ž๊ฐ€ ์ž‘์€ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์šฐ์„ ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„ ์ •๋ ฌ ํ›„ ํ๋กœ ์ „ํ™˜ํ•œ๋‹ค! - ํ์— ๊ฐ’์ด ์—†์–ด์งˆ ๋•Œ๊นŒ์ง€ ๊บผ๋‚ด ์ƒํ•˜์ขŒ์šฐ์˜ ๊ฐ ๊ฐ’์ด ๋‹ค์‹œ ํ์— ๋„ฃ๋Š” ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. - ํ์—์„œ ๊บผ๋‚ธ ๊ฐ’์˜ ์‹œ๊ฐ„์ด S์™€ ๊ฐ™๋‹ค๋ฉด ์ข…๋ฃŒ - X-1 ํ–‰ ..

    [Python] ๋ฐฑ์ค€ 14502 ์—ฐ๊ตฌ์†Œ | BFS

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

    [Python] ๋ฐฑ์ค€ 4485 ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€? | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/4485 4485๋ฒˆ: ๋…น์ƒ‰ ์˜ท ์ž…์€ ์• ๊ฐ€ ์ ค๋‹ค์ง€? ์ ค๋‹ค์˜ ์ „์„ค ๊ฒŒ์ž„์—์„œ ํ™”ํ์˜ ๋‹จ์œ„๋Š” ๋ฃจํ”ผ(rupee)๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๊ฐ„ํ˜น '๋„๋‘‘๋ฃจํ”ผ'๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฒ€์ •์ƒ‰ ๋ฃจํ”ผ๋„ ์กด์žฌํ•˜๋Š”๋ฐ, ์ด๊ฑธ ํš๋“ํ•˜๋ฉด ์˜คํžˆ๋ ค ์†Œ์ง€ํ•œ ๋ฃจํ”ผ๊ฐ€ ๊ฐ์†Œํ•˜๊ฒŒ ๋œ๋‹ค! ์ ค๋‹ค์˜ ์ „์„ค ์‹œ๋ฆฌ์ฆˆ์˜ ์ฃผ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ ์œ„์น˜๋ณ„ (0, 0)์—์„œ ์ตœ์†Œ ๋น„์šฉ์„ ๊ณ„์‚ฐ - dr, dc : ๊ฐ ์œ„์น˜์—์„œ ์•„๋ž˜, ์œ„, ์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉํ–ฅํ‚ค - graph : ๊ฐ ์œ„์น˜ ๋ฐฉ๋ฌธ์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋น„์šฉ - distance : ๊ฐ ์œ„์น˜์—์„œ (0, 0)๊นŒ์ง€ ๊ฐ€๊ธฐ์œ„ํ•œ ์ตœ์†Œ ๋น„์šฉ, ์ดˆ๊ธฐ๋Š” ๋ฌดํ•œ์ˆ˜ ์ž‘์„ฑ ์ฝ”๋“œ imp..

    [Python] ๋ฐฑ์ค€ 1504 ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1504 1504๋ฒˆ: ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, a๋ฒˆ ์ •์ ์—์„œ b๋ฒˆ ์ •์ ๊นŒ์ง€ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ์กด www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - v1 -> v2 ์ˆœ์„œ ๋˜๋Š” v2 -> v1 ์ˆœ์„œ ๊ฐ๊ฐ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜์—ฌ ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. (2๋ฒˆ์˜ ๋‹ค์ต์ŠคํŠธ๋ผ) - ๊ฒฝ๋กœ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ(๊ฒฐ๊ณผ ๊ฐ’์ด INF๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ) -1์„ ์ถœ๋ ฅ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ฝ”๋“œ ์ฃผ์„ ์ฐธ๊ณ  ์ž‘์„ฑ ์ฝ”๋“œ import sys import heapq input =..

    [Python] ๋ฐฑ์ค€ 13549 ์ˆจ๋ฐ”๊ผญ์งˆ 3 | ๋‹ค์ต์ŠคํŠธ๋ผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/13549 13549๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 3 ์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 ≤ N ≤ 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 ≤ K ≤ 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ www.acmicpc.net ์ž‘์„ฑ ์ฝ”๋“œ import heapq INF = int(1e9) N, K = map(int, input().split()) # ์‹œ์ž‘ ์œ„์น˜, ๋„์ฐฉ ์œ„์น˜ distance = [INF]*100001 # 100001๊ฐœ์˜ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ def dijkstra(start): # ๋‹ค์ต์ŠคํŠธ๋ผ distance[start] = 0 # ์‹œ์ž‘..

    [Python] ๋ฐฑ์ค€ 1916 ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ | ๋‹ค์ต์ŠคํŠธ๋ผ | ์‹œ๊ฐ„์ดˆ๊ณผ

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://www.acmicpc.net/problem/1916 1916๋ฒˆ: ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ ์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ M+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ www.acmicpc.net ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ๋ฐ›์„ ์ž…๋ ฅ์ด ๋งŽ์œผ๋ฏ€๋กœ sys.stdin.realine ์„ ์‚ฌ์šฉํ•œ๋‹ค. (์‹œ๊ฐ„ ์ดˆ๊ณผ ํ•ด๊ฒฐ) - ์ „ํ˜•์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ ์œ ํ˜•์ด๋‹ค - ์šฐ์„ ์ˆœ์œ„ ํ(์ตœ์†Œํž™)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์†Œ ๊ฑฐ๋ฆฌ ๋…ธ๋“œ๋ฅผ ์„ ํƒ - distance : start ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๊ฐ ๋…ธ๋“œ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ €์žฅ (INF ๋กœ ์ดˆ๊ธฐํ™”) - graph : ๊ฐ ..