-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aba355
commit 71ba4bf
Showing
3 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
""" |