diff --git "a/tgyuuAn/DFS/\353\223\261\354\202\260 \353\247\210\353\213\210\354\225\204.py" "b/tgyuuAn/DFS/\353\223\261\354\202\260 \353\247\210\353\213\210\354\225\204.py" new file mode 100644 index 00000000..7af2aa8f --- /dev/null +++ "b/tgyuuAn/DFS/\353\223\261\354\202\260 \353\247\210\353\213\210\354\225\204.py" @@ -0,0 +1,31 @@ +import sys +sys.setrecursionlimit(10 ** 6) + +def input(): return sys.stdin.readline().rstrip() + +N = int(input()) +# N = 30 만 -> O(N*log(N)) + +tree = [[] for _ in range(N+1)] +for _ in range(N-1): + node1, node2 = map(int, input().split()) + tree[node1].append(node2) + tree[node2].append(node1) + +answer = 0 +def dfs(now_idx, visited, tree): + global answer + + temp_cnt = 1 + for next_node in tree[now_idx]: + if next_node in visited: continue + visited.add(next_node) + temp = dfs(next_node, visited, tree) + answer += temp * (temp-1) // 2 + answer += temp * (N-temp) + temp_cnt += temp + + return temp_cnt + +dfs(1,{1,}, tree) +print(answer) diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 6e403df7..aea5b0d5 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -78,4 +78,5 @@ | 69차시 | 2024.08.10 | 누적합, 수학 | 1의 개수 세기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/228 | 70차시 | 2024.08.16 | 스택 | 탑 보기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232 | 71차시 | 2024.08.20 | 다익스트라 | 다익스트라 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/235 +| 72차시 | 2024.08.23 | DFS + 트리 | 등산 마니아 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/238 ---