Skip to content

Commit

Permalink
22차시 문제 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
alstjr7437 committed Apr 13, 2024
1 parent 2aba355 commit 71ba4bf
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
44 changes: 44 additions & 0 deletions alstjr7437/BFS/연결-요소의-개수.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Foundation

let input = readLine()!.split(separator:
" ").map{ Int($0)!}

let n = input[0] , m = input[1]

var graph : [[Int]] = Array(repeating: [], count: n + 1)
var visited : [Bool] = Array(repeating: false, count: n + 1)
var result : Int = 0

for _ in 0..<m {
let tmp = readLine()!.split(separator: " ").map { Int($0)!}
graph[tmp[0]].append(tmp[1])
graph[tmp[1]].append(tmp[0])
}

func bfs(start: Int){
visited[start] = true
var queue: [Int] = [start]

var idx : Int = 0

while idx < queue.count{
let current = queue[idx]

idx += 1
for i in graph[current]{
if visited[i] == false {
queue.append(i)
visited[i] = true
}
}
}
}

for i in 1...n{
if visited[i] == false {
bfs(start : i)
result += 1
}
}

print(result)
3 changes: 2 additions & 1 deletion alstjr7437/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
| 17차시 | 2024.03.16 | DP | <a href="https://www.acmicpc.net/problem/2228">구간 나누기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/61 |
| 18차시 | 2024.03.23 | 다익스트라 | <a href="https://www.acmicpc.net/problem/1753">최단 경로</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/66 |
| 19차시 | 2024.03.27 | 문자열 | <a href="https://www.acmicpc.net/problem/5525">IOIOI</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/67 |
| 20차시 | 2024.04.03 | BFS | <a href="https://www.acmicpc.net/problem/1697">숨바꼭질</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/70 |
| 20차시 | 2024.04.03 | BFS | <a href="https://www.acmicpc.net/problem/1697">숨바꼭질</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/70 |
| 22차시 | 2024.04.13 | BFS | <a href="https://www.acmicpc.net/problem/11724">연결 요소의 개수</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-6/pull/78 |
85 changes: 85 additions & 0 deletions alstjr7437/비트마스킹/집합.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import sys

input = sys.stdin.readline

m = int(input())

result = 0

for _ in range(m):
cmd = input().split()

if cmd[0] == "add":
result |= (1 << int(cmd[1]))

if cmd[0] == "remove":
result &= ~(1 << int(cmd[1]))

if cmd[0] == "check":
if result & (1 << int(cmd[1])):
print(1)
else:
print(0)

if cmd[0] == "toggle":
result ^= (1 << int(cmd[1]))

if cmd[0] == "all":
result = (1 << 21) - 1

if cmd[0] == "empty":
result = 0


# for _ in range(m):
# cmd = input().split()

# if cmd[0] == "add":
# result[int(cmd[1])] = 1

# if cmd[0] == "remove":
# result[int(cmd[1])] = 0

# if cmd[0] == "check":
# if result[int(cmd[1])] == 1:
# print(1)
# else :
# print(0)

# if cmd[0] == "toggle":
# if result[int(cmd[1])] == 1:
# result[int(cmd[1])] = 0
# else :
# result[int(cmd[1])] = 1

# if cmd[0] == "all":
# result = [1] * len(result)

# if cmd[0] == "empty":
# result = [0] * len(result)

"""
S = set()
for _ in range(m):
temp = input().split()
if temp[0] == "add":
S.add(int(temp[1]))
if temp[0] == "remove":
S.discard(int(temp[1]))
if temp[0] == "check":
if int(temp[1]) in S:
print(1)
else :
print(0)
if temp[0] == "toggle":
if int(temp[1]) in S:
S.discard(int(temp[1]))
else:
S.add(int(temp[1]))
if temp[0] == "all":
S = set([i for i in range(1, 21)])
if temp[0] == "empty":
S = set()
"""

0 comments on commit 71ba4bf

Please sign in to comment.