ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17089๋ฒˆ: ์„ธ ์นœ๊ตฌ

์ฒซ์งธ ์ค„์— ์‚ฌ๋žŒ์˜ ์ˆ˜ N(3 ≤ N ≤ 4,000), ์นœ๊ตฌ ๊ด€๊ณ„์˜ ์ˆ˜ M(0 ≤ M ≤ 4,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ์˜๋ฏธํ•˜๋Š” ๋‘ ์ •์ˆ˜ A, B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์นœ๊ตฌ ๊ด€๊ณ„๋Š” A์™€ B, ๊ทธ๋ฆฌ๊ณ  B์™€ A๊ฐ€ ์นœ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌธ์ œ์—์„œ๋Š” N๋ช…์˜ ์‚ฌ๋žŒ์ด ์žˆ๊ณ , ์„ธ ์‚ฌ๋žŒ์„ ์„ ํƒํ•  ๋•Œ ์„ธ ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ์นœ๊ตฌ์ด๊ณ  ์„ธ ์‚ฌ๋žŒ์˜ ์นœ๊ตฌ ์ˆ˜์˜ ํ•ฉ์ด ์ตœ์†Œ๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๋ฌธ์ œ์˜ ์กฐ๊ฑด์—๋Š” ์„ธ ์‚ฌ๋žŒ์„ ์„ ํƒํ•  ๊ฒฝ์šฐ ์„ธ ์‚ฌ๋žŒ์€ ์นœ๊ตฌ์—์„œ ์ œ์™ธํ•ด์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์ด ์žˆ๋‹ค. ์ฒ˜์Œ์—๋Š” ๋ฌธ์ œ๋ฅผ `DFS`๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•ด์•ผ ํ•˜์ง€ ์•Š๋‚˜ ๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€๋Š”๋ฐ ์„ธ ์‚ฌ๋žŒ์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์„ `3์ค‘ ๋ฐ˜๋ณต๋ฌธ`์„ ํ†ตํ•ด ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from math import inf


if __name__ == '__main__':
    answer = inf
    n, m = map(int, stdin.readline().split())
    relation = [[False] * (n + 1) for _ in range(n + 1)]
    friends = [0] * (n + 1)

    # ์นœ๊ตฌ ๊ด€๊ณ„์™€ ์นœ๊ตฌ ์ˆ˜๋ฅผ ์ €์žฅํ•จ.
    for i in range(m):
        me, my_friend = map(int, stdin.readline().split())
        relation[me][my_friend] = True
        relation[my_friend][me] = True
        friends[me] += 1
        friends[my_friend] += 1

    # 3์ค‘ for ๋ฌธ์œผ๋กœ ์นœ๊ตฌ ์„ธ ๋ช…์„ ์„ ํƒํ•˜์—ฌ ํƒ์ƒ‰.
    for i in range(n + 1):
        for j in range(i + 1, n + 1):
            # ์ฒซ๋ฒˆ์งธ ์นœ๊ตฌ ๋งŒ์กฑ.
            if relation[i][j]:
                for k in range(j + 1, n + 1):
                    # ๋‘๋ฒˆ์งธ, ์„ธ๋ฒˆ์งธ ์นœ๊ตฌ ๋งŒ์กฑ
                    if relation[i][k] and relation[j][k]:
                        # [B, C], [A, C], [A, B]์™€ ๊ฐ™์ด ์—ฐ๊ฒฐ๋œ ์นœ๊ตฌ 6๋ช…์„ ์ œ์™ธํ•ด์•ผ ํ•จ.
                        answer = min(answer, friends[i] + friends[j] + friends[k] - 6)

    print(-1 if answer == inf else answer)

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€