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

72-tgyuuAn #238

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -78,4 +78,5 @@
| 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
| 71μ°¨μ‹œ | 2024.08.20 | λ‹€μ΅μŠ€νŠΈλΌ | <a href="https://www.acmicpc.net/problem/24042">λ‹€μ΅μŠ€νŠΈλΌ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/235
| 72μ°¨μ‹œ | 2024.08.23 | DFS + 트리 | <a href="https://www.acmicpc.net/problem/20188">λ“±μ‚° λ§ˆλ‹ˆμ•„</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/238
---