[BOJ][๐ŸŸก5][๋ฐฑ์ค€#13023] ABCDE

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #13023


๋ฌธ์ œ

BOJ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์บ ํ”„์—๋Š” ์ด N๋ช…์ด ์ฐธ๊ฐ€ํ•˜๊ณ  ์žˆ๋‹ค. ์‚ฌ๋žŒ๋“ค์€ 0๋ฒˆ๋ถ€ํ„ฐ N-1๋ฒˆ์œผ๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ , ์ผ๋ถ€ ์‚ฌ๋žŒ๋“ค์€ ์นœ๊ตฌ์ด๋‹ค. ์˜ค๋Š˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง„ ์‚ฌ๋žŒ A, B, C, D, E๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ๊ตฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

A๋Š” B์™€ ์นœ๊ตฌ๋‹ค. B๋Š” C์™€ ์นœ๊ตฌ๋‹ค. C๋Š” D์™€ ์นœ๊ตฌ๋‹ค. D๋Š” E์™€ ์นœ๊ตฌ๋‹ค.

์œ„์™€ ๊ฐ™์€ ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์•ˆํ•˜๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฌ๋žŒ์˜ ์ˆ˜ N (5 โ‰ค N โ‰ค 2000)๊ณผ ์นœ๊ตฌ ๊ด€๊ณ„์˜ ์ˆ˜ M (1 โ‰ค M โ‰ค 2000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ์ •์ˆ˜ a์™€ b๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, a์™€ b๊ฐ€ ์นœ๊ตฌ๋ผ๋Š” ๋œป์ด๋‹ค. (0 โ‰ค a, b โ‰ค N-1, a โ‰  b) ๊ฐ™์€ ์นœ๊ตฌ ๊ด€๊ณ„๊ฐ€ ๋‘ ๋ฒˆ ์ด์ƒ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.


์ถœ๋ ฅ

๋ฌธ์ œ์˜ ์กฐ๊ฑด์— ๋งž๋Š” A, B, C, D, E๊ฐ€ ์กด์žฌํ•˜๋ฉด 1์„ ์—†์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

5 4
0 1
1 2
2 3
3 4


์ถœ๋ ฅ

1


์˜ˆ์ œ 2

์ž…๋ ฅ

5 5
0 1
1 2
2 3
3 0
1 4


์ถœ๋ ฅ

1


์˜ˆ์ œ 3

์ž…๋ ฅ

6 5
0 1
0 2
0 3
0 4
0 5


์ถœ๋ ฅ

0


์˜ˆ์ œ 4

์ž…๋ ฅ

8 8
1 7
3 7
4 7
3 4
4 6
3 5
0 4
2 7


์ถœ๋ ฅ

1


My Sol

import sys
input = sys.stdin.readline

def dfs(u, i):
    global stack, arr
    if i == 5: return True
    for v in arr[u]:
        if v in stack: continue
        stack.append(v)
        if dfs(v, i+1): return True
        stack.pop()
    return False

N, M = map(int, input().split())
arr = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(int, input().split())
    arr[u].append(v)
    arr[v].append(u)

stack = []
ans = 0
for i in range(N):
    if dfs(i, 0):
        ans = 1
        break

print(ans)

dfs๋ฅผ ํ™œ์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

  1. ๋ชจ๋“  ๊ด€๊ณ„๋ฅผ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ•˜์—ฌ 2์ฐจ์› ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
  2. 0๋ถ€ํ„ฐ N-1๊นŒ์ง€ ๋ชจ๋“  ์ง€์ ์„ 0๋ถ€ํ„ฐ ํ•˜์—ฌ dfs๋ฅผ ์‹ค์‹œํ•œ๋‹ค.
  3. dfs๋Š” ์‹œ์ž‘์ ์ธ u์™€ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  v์— ๋Œ€ํ•˜์—ฌ stack์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ๊ด€๊ณ„์ธ ๊ฒฝ์šฐ ๋” ๊นŠ์ด๋ฅผ ๊นŠ๊ฒŒ ํ•˜์—ฌ ์—ฐ๊ฒฐํ•ด๋‚˜๊ฐ„๋‹ค.
  4. stack์ด ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”, ์ฆ‰ 5๊ฐœ์˜ ๊ด€๊ณ„ ๊นŠ์ด๊นŒ์ง€ ๋‚ด๋ ค๊ฐ”๋‹ค๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  5. ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ œ์˜ ์š”๊ตฌ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•œ ๊ฒƒ์ด๋ฏ€๋กœ ans๋ฅผ 1๋กœ ๋ฐ”๊พธ๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.
  6. ans๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


์˜๋ฌธ์ 

์‚ฌ์‹ค Node.js๋กœ ๋ฌธ์ œ๋ฅผ ๋จผ์ € ํ’€์—ˆ์—ˆ๋‹ค.

const fs = require("fs");
const [NM, ...rels] = fs
  .readFileSync('/dev/stdin')
  // .readFileSync("./t.txt")
  .toString()
  .split("\n")
  .map((a) => a.trim());

const [N, M] = NM.split(" ").map(Number);
const arr = [];
for (let i = 0; i < M; i++) {
  arr.push([]);
}
rels.forEach((rel) => {
  const [u, v] = rel.split(" ").map(Number);
  arr[u].push(v);
  arr[v].push(u);
});

const stack = [];
const dfs = (u, n) => {
  if (n === 5) return true;
  for (v of arr[u]) {
    if (stack.includes(v)) continue;
    stack.push(v);
    if (dfs(v, n + 1)) return true;
    stack.pop();
  }
};

let ret = 0;
for (let i = 0; i < N; i++) {
  if (dfs(i, 0)) {
    ret = 1;
    break;
  }
}
console.log(ret);

๊ฑฐ์˜ ๋˜‘๊ฐ™์€ ๋ฐฉ์‹์ธ๋ฐ TypeError๋ฅผ ๊ฒช์œผ๋ฉฐ ๋ฌธ์ œ๋ฅผ ์ž๊พธ ํ‹€๋ ธ๋‹ค. ๊ทธ๋ž˜์„œ Python์œผ๋กœ ๋˜‘๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ๋ฐ”๋กœ ๋งž์•˜๋‹ค๊ณ  ํ•ด์„œ ์–ด์ด๊ฐ€ ์—†์—ˆ๋‹ค. ๋งˆ์Œ์ด ์•„ํ”„๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ