๐Ÿ“๋ฌธ์ œ ํ’€์ด

    [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..

    [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] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆœ์œ„ | ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

    ~๋ชฉ์ฐจ~ ๋ฌธ์ œ ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ ์ž‘์„ฑ ์ฝ”๋“œ ๋ฌธ์ œ https://school.programmers.co.kr/learn/courses/30/lessons/49191 ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”. programmers.co.kr ๋ฌธ์ œ ํ•ด๊ฒฐ ํฌ์ธํŠธ - ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ œ - ์ •์  i๊ฐ€ 1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋ชจ๋“  ์ •์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด ์ˆœ์œ„๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. - ์ดˆ๊ธฐ ์ธ์ ‘ ํ–‰๋ ฌ์€ ๋ฌดํ•œ์ˆ˜๋กœ ์ดˆ๊ธฐํ™”, ์—ฐ๊ด€์ด ์žˆ๋Š” ๋‘ ์ •์  results๋Š” 1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ์ด๋Š” 1๋ฒˆ์— ๊ฑธ์ณ a ์ •์ ๊ณผ b ์ •์ ์ด ๊ด€๊ณ„๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค. - a ์ •์ ์—์„œ b ์ •์ ์œผ๋กœ ๋ฐ”๋กœ ๊ฐ€๋Š” ๋ฐฉ๋ฒ• (a..

    [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 : ํ•ด๋‹น..