Skip to content

Commit

Permalink
Merge pull request #189 from AlgoLeadMe/55-tgyuuAn
Browse files Browse the repository at this point in the history
55-tgyuuAn
  • Loading branch information
tgyuuAn authored May 22, 2024
2 parents abb7a08 + 81ccc05 commit 8da6d44
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@
| 48์ฐจ์‹œ | 2024.03.25 | ๋ฒจ๋งŒ ํฌ๋“œ | <a href="https://www.acmicpc.net/problem/1738">๊ณจ๋ชฉ๊ธธ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/171
| 49์ฐจ์‹œ | 2024.03.29 | DP | <a href="https://www.acmicpc.net/problem/10217">KCM Travel</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/174
| 50์ฐจ์‹œ | 2024.04.01 | BFS | <a href="https://www.acmicpc.net/problem/9328">์—ด์‡ </a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175
| 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
| 51์ฐจ์‹œ | 2024.04.07 | BFS | <a href="https://www.acmicpc.net/problem/5213">๊ณผ์™ธ๋งจ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/179
| 52์ฐจ์‹œ | 2024.05.06 | ์œ„์ƒ์ •๋ ฌ | <a href="https://www.acmicpc.net/problem/1516">๊ฒŒ์ž„ ๊ฐœ๋ฐœ</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/182
| 53์ฐจ์‹œ | 2024.05.09 | ๋ฐฑํŠธ๋ž˜ํ‚น | <a href="https://www.acmicpc.net/problem/12100">2048 (Easy)</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/184
| 54์ฐจ์‹œ | 2024.05.14 | ๋‹ค์ต์ŠคํŠธ๋ผ | <a href="https://www.acmicpc.net/problem/9370">๋ฏธํ™•์ธ ๋„์ฐฉ์ง€</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/185
| 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 8da6d44

Please sign in to comment.