Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

13 ljedd2 #167

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

13 ljedd2 #167

wants to merge 12 commits into from

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Sep 27, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ ํ”Œ๋กœ์ด๋“œ ์›Œ์ƒฌ | BOJ 1058 ์นœ๊ตฌ

๋ฌธ์ œ ์„ค๋ช…
์ง€๋ฏผ์ด๋Š” ์„ธ๊ณ„์—์„œ ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์‚ฌ๋žŒ์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ ์‚ฌ๋žŒ์˜ 2-์นœ๊ตฌ๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. ์–ด๋–ค ์‚ฌ๋žŒ A๊ฐ€ ๋˜๋‹ค๋ฅธ ์‚ฌ๋žŒ B์˜ 2-์นœ๊ตฌ๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด์„ , ๋‘ ์‚ฌ๋žŒ์ด ์นœ๊ตฌ์ด๊ฑฐ๋‚˜, A์™€ ์นœ๊ตฌ์ด๊ณ , B์™€ ์นœ๊ตฌ์ธ C๊ฐ€ ์กด์žฌํ•ด์•ผ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์‚ฌ๋žŒ์€ 2-์นœ๊ตฌ์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ๋žŒ์ด๋‹ค. ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์‚ฌ๋žŒ์˜ 2-์นœ๊ตฌ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ œํ•œ ์กฐ๊ฑด

  • N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜

๐Ÿ’ช A์™€ B๊ฐ€ ์นœ๊ตฌ๋ฉด, B์™€ A๋„ ์นœ๊ตฌ์ด๊ณ , A์™€ A๋Š” ์นœ๊ตฌ๊ฐ€ ์•„๋‹ˆ๋‹ค.

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

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

๋””์Šค์ฝ”๋“œ์— ํ•ด๋‹น ๋‚ด์šฉ์— ๋Œ€ํ•œ ๋…ธํŠธ๋ฅผ ์ •๋ฆฌํ•ด๋‘” ๊ฒƒ์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

(A์˜ 2-์นœ๊ตฌ) = (A์˜ ์นœ๊ตฌ) U (์นœ๊ตฌ์˜ ์นœ๊ตฌ) ์ด๋ฏ€๋กœ
ํ•œ ๋ฒˆ ๊ฑด๋„ˆ์„œ ์นœ๊ตฌ์ธ ์‚ฌ๋žŒ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ ํ›„ ๊ฑฐ๋ฆฌ (visited) ๊ฐ’์ด 2 ์ดํ•˜๊ฐ€ ๋œ๋‹ค๋ฉด 2-์นœ๊ตฌ๋ฅผ ๋งŒ์กฑํ•ฉ๋‹ˆ๋‹ค.
2-์นœ๊ตฌ์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‚ฌ๋žŒ์„ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n = int(input())
graph = [list(input().strip()) for _ in range(n)]
visited = [[0] * n for _ in range(n)]

for k in range(n):
    for i in range(n):
        for j in range(n):
            if i == j:
                continue

        # 2-์นœ๊ตฌ์ธ ๊ฒฝ์šฐ
            if graph[i][j] == 'Y' or (graph[i][k] == 'Y' and graph[k][j] == 'Y'):
                visited [i][j] = 1

res = 0
for row in visited:
  res = max(res,sum(row))
print(res)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์•„์ง ์ €์—๊ฒŒ ๋„ˆ๋ฌด๋‚˜๋‘ ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ๋ฐ ๋””์Šค์ฝ”๋“œ์— ์˜ฌ๋ ค์ฃผ์‹  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋…ธํŠธ ๋•๋ถ„์— ๊ฐœ๋…์ด ํ™•์‹คํ•˜๊ฒŒ ์žกํ˜”๋„ค์š”! ๊ทธ์น˜๋งŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ดํ•ดํ•ด๋„ ์ด ๋ฌธ์ œ์˜ ์ง€๋ฌธ์„ ๋ณด์•˜์„ ๋•Œ ์–ด๋Š ํฌ์ธํŠธ๊ฐ€ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์„ ์ƒ๊ฐํ•˜๊ฒŒ ๋งŒ๋“œ๋Š”์ง€ ๊ฐ์„ ๋ชป์žก์•„์„œ ๊ฒฐ๊ตญ ์ •์€๋‹˜์˜ ์ˆ˜๋„์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค๐Ÿฅบ

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ถ€ํ„ฐ ํ•œ๋ฒˆ ์ œ๋Œ€๋กœ ๋ฟŒ์ˆ˜๊ณ  ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์„ ๊ฑด๋“œ๋ ค๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค๐Ÿ˜–

์ด ๋ฌธ์ œ๋ฅผ bfs๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์Šต๋‹ˆ๋‹ค!

import sys
from collections import deque

def bfs(start):
    visited = [False] * n
    # ์‹œ์ž‘ ๋…ธ๋“œ์™€ ๊นŠ์ด๋ฅผ ๊ฐ™์ด ํ์— ์ €์žฅํ•จ
    queue = deque([[start, 0]])
    visited[start] = True
    friends_count = 0

    while queue:
        current, depth = queue.popleft()

        # ๊นŠ์ด๊ฐ€ 2 ์ด์ƒ์ธ ๊ฒฝ์šฐ๋Š” ๋ฌด์‹œํ•จ
        if depth >= 2:  
            continue

        for neighbor in range(n):
            if not visited[neighbor] and graph[current][neighbor] == "Y":
		# ์นœ๊ตฌ ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ด
                friends_count += 1
		# ๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ํ•จ
                visited[neighbor] = True
		# ์ด์›ƒ๊ณผ ๊นŠ์ด๋ฅผ ์ถ”๊ฐ€ํ•จ
                queue.append([neighbor, depth + 1])

    return friends_count

# ๋…ธ๋“œ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•จ
n = int(sys.stdin.readline().strip())
# ๊ทธ๋ž˜ํ”„ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•จ
graph = [list(sys.stdin.readline().strip()) for _ in range(n)]
max_friends = 0 

for i in range(n):
    # ๊ฐ ๋…ธ๋“œ๋ณ„๋กœ ์ตœ๋Œ€ ์นœ๊ตฌ ์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•จ
    max_friends = max(max_friends, bfs(i))

# ์ตœ๋Œ€ ์นœ๊ตฌ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•จ
print(max_friends)

๋‹ค์Œ PR๋„ ํŒŸํŒ…์ž…๋‹ˆ๋‹ค๐Ÿ€

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋…ธํŠธ ๋•๋ถ„์— ํ—ท๊ฐˆ๋ฆฌ๋˜ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ฐœ๋…์„ ๋ช…ํ™•ํžˆ ์•Œ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์•„์š”!
๋ฌธ์ œ ๋‚ด์šฉ์„ ๋ณด๊ณ  ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ์ง€ ๋ฐ”๋กœ ๋– ์˜ฌ๋ฆฌ์‹  ๊ฒŒ ๋Œ€๋‹จํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค...! ๐Ÿฅน

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants