Skip to content

Commit

Permalink
2024-05-18
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed May 17, 2024
1 parent 261aa3f commit 71eed4e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@
| 51์ฐจ์‹œ | 2023.04.07 | BFS | <a href="https://www.acmicpc.net/problem/5213">๊ณผ์™ธ๋งจ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179
| 52์ฐจ์‹œ | 2023.05.06 | ์œ„์ƒ์ •๋ ฌ | <a href="https://www.acmicpc.net/problem/1516">๊ฒŒ์ž„ ๊ฐœ๋ฐœ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182
| 53์ฐจ์‹œ | 2023.05.09 | ๋ฐฑํŠธ๋ž˜ํ‚น | <a href="https://www.acmicpc.net/problem/12100">2048 (Easy)</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184
| 55์ฐจ์‹œ | 2023.05.18 | ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ | <a href="https://www.acmicpc.net/problem/4195">์นœ๊ตฌ ๋„คํŠธ์›Œํฌ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/189
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
from collections import defaultdict

def input(): return sys.stdin.readline().rstrip()

def find_parent(element, graph):
if graph[element] == element: return element

parent = graph[element]
graph[element] = find_parent(parent, graph)
return graph[element]

def union(first, second, graph, count):
x = find_parent(first, graph)
y = find_parent(second, graph)

if x == y: return

x, y = min(x, y), max(x, y)
graph[y] = x
count[x] += count[y]
count[y] = 0
return

T = int(input())

for _ in range(T):
F = int(input())
index = 1
parent = defaultdict(str)
count = defaultdict(lambda : 1)

for _ in range(F):
first, second = input().split()
if parent[first] == "": parent[first] = first
if parent[second] == "": parent[second] = second
if parent[first] != parent[second]: union(first, second, parent, count)

print(count[find_parent(first, parent)])

0 comments on commit 71eed4e

Please sign in to comment.