Skip to content

Commit

Permalink
2024-08-23
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Aug 25, 2024
1 parent 81f4b15 commit 6855b04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tgyuuAn/DFS/λ“±μ‚° λ§ˆλ‹ˆμ•„.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@
| 68μ°¨μ‹œ | 2024.08.06 | 그리디 | <a href="https://www.acmicpc.net/problem/24337">가희와 탑</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/226
| 69μ°¨μ‹œ | 2024.08.10 | λˆ„μ ν•©, μˆ˜ν•™ | <a href="https://www.acmicpc.net/problem/9527">1의 개수 μ„ΈκΈ°</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/228
| 70μ°¨μ‹œ | 2024.08.16 | μŠ€νƒ | <a href="https://www.acmicpc.net/problem/22866">탑 보기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/232
| 72μ°¨μ‹œ | 2024.08.23 | DFS + 트리 | <a href="https://www.acmicpc.net/problem/20188">λ“±μ‚° λ§ˆλ‹ˆμ•„</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/238
---

0 comments on commit 6855b04

Please sign in to comment.