From 3fd5c15a3035907d07e0c05f72f08d5426fd14f2 Mon Sep 17 00:00:00 2001 From: eclir Date: Fri, 25 Mar 2022 23:30:50 +0900 Subject: [PATCH 1/2] 0325 --- ...\354\236\220 \354\271\264\353\223\2342.py" | 53 +++++++++++++++++++ ..._\354\210\230\354\240\225 \354\240\204.py" | 29 ++++++++++ ...]\354\210\230 \354\260\276\352\270\260.py" | 47 ++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 "eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342.py" create mode 100644 "eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342_\354\210\230\354\240\225 \354\240\204.py" create mode 100644 "eclir/BOJ/SILVER/[1920]\354\210\230 \354\260\276\352\270\260.py" diff --git "a/eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342.py" "b/eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342.py" new file mode 100644 index 0000000..1e07148 --- /dev/null +++ "b/eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342.py" @@ -0,0 +1,53 @@ +''' +{입력} + +''' +import sys +input = sys.stdin.readline + +origin_number = int(input()) #상근이가 가지고 있는 카드 개수 +card_origin = sorted(list(map(int, input().split()))) #상근이가 가지고 있는 카드들 +compare_number = int(input()) #비교해볼 카드의 개수 +card_compare = list(map(int, input().split())) #각각 비교해볼 카드들 + + + +''' +{이분 탐색 재귀 함수} + +상근이가 가지고 있는 카드가 target값을 포함할 때 개수를 반환. +''' +def binarySearch(array, target, left, right): + if left > right: #예외 경우 처리 + return 0 + middle_idx = (left + right) // 2 + middle = array[middle_idx] + + if middle < target: + return(binarySearch(array, target, middle_idx + 1, right)) + elif middle > target: + return(binarySearch(array, target, left, middle_idx - 1)) + else: #middle = target 인 경우 + return array[left:right+1].count(target) #middle(=target) 값을 포함하는 그 때의 범위(인덱스 : left ~ right) 안에서 target과 같은 값의 개수를 세준다. + + + +''' +{메인} + +비교할 카드 각각마다의 개수를 저장하는 딕셔너리를 만든다. +''' +dic = {} +for i in sorted(card_compare): # + if i not in dic: #아직 이 카드의 개수가 딕셔너리에 저장되지 않았을 때. + dic[i] = binarySearch(card_origin, i, 0, origin_number - 1) + + +''' +{출력} + +비교할 카드들 순서대로 돌아가는 for문 : + if 우리가 만든 딕셔너리 안에 그 카드의 숫자가 있다면, 그 때의 값(숫자의 개수)을 출력 + else 딕셔너리 안에 해당값이 존재하지 않는다면, '0'을 출력 +''' +print(' '.join(str(dic[x]) if x in dic else "0" for x in card_compare)) \ No newline at end of file diff --git "a/eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342_\354\210\230\354\240\225 \354\240\204.py" "b/eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342_\354\210\230\354\240\225 \354\240\204.py" new file mode 100644 index 0000000..768f287 --- /dev/null +++ "b/eclir/BOJ/SILVER/[10816]\354\210\253\354\236\220 \354\271\264\353\223\2342_\354\210\230\354\240\225 \354\240\204.py" @@ -0,0 +1,29 @@ +from sys import stdin +origin_number = int(stdin.readline()) +card_origin = sorted(list(map(int, stdin.readline().split()))) +compare_number = int(stdin.readline()) +card_compare = list(map(int, stdin.readline().split())) + +def binarySearch(array, target, left, right): + if left > right: + return 0 + middle_idx = (left + right) // 2 + middle = array[middle_idx] + + if middle < target: + return(binarySearch(array, target, middle_idx + 1, right)) + elif middle > target: + return(binarySearch(array, target, left, middle_idx - 1)) + else: + return array[left:right+1].count(target) + +dic = {} +for i in card_origin: + if i not in dic: + dic[i] = binarySearch(card_origin, i, 0, origin_number) + +for i in range(compare_number): + if card_compare[i] in dic: + print(dic[card_compare[i]], end = ' ') + else: + print(0, end = ' ') \ No newline at end of file diff --git "a/eclir/BOJ/SILVER/[1920]\354\210\230 \354\260\276\352\270\260.py" "b/eclir/BOJ/SILVER/[1920]\354\210\230 \354\260\276\352\270\260.py" new file mode 100644 index 0000000..0c8b7ed --- /dev/null +++ "b/eclir/BOJ/SILVER/[1920]\354\210\230 \354\260\276\352\270\260.py" @@ -0,0 +1,47 @@ +''' +{이분 탐색 재귀 함수} + +middle:중간값과 타겟의 크기를 비교하여 비교 범위를 줄여나간다. +만약 타겟값이 array에 존재한다면 1을 반환하고 +그렇지 않다면 0을 반환한다. +''' + +def binarySearch(array, target, left, right): + if left > right: #예외 처리. target값이 array에 없을 때 0을 반환. + return(0) + middle_idx = (left + right) // 2 + middle = array[middle_idx] + + #array는 오름차순으로 정렬된 상태. 중간값을 기준으로 비교할 범위를 새로 선정. + if middle < target: + return(binarySearch(array, target, middle_idx + 1, right)) + elif middle > target: + return(binarySearch(array, target, left, middle_idx - 1)) + else: + return (1) + + + +''' +{입력} + +''' +list_origin = [] +list_compare = [] + +length_origin = int(input()) +list_origin = list(map(int, input().split())) +list_origin.sort() #오름차순으로 정렬 + +length_compare = int(input()) +list_compare = list(map(int, input().split())) + + + +''' +{출력} + + +''' +for i in range(length_compare): + print(binarySearch(list_origin, list_compare[i], 0, length_origin - 1)) \ No newline at end of file From 9494419c04798e6ec7f09179500dcbb484482fcb Mon Sep 17 00:00:00 2001 From: eclir Date: Sat, 2 Apr 2022 00:36:55 +0900 Subject: [PATCH 2/2] =?UTF-8?q?3=EC=A3=BC=EC=B0=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BOJ/SILVER/[1260]DFS\354\231\200BFS.py" | 77 +++++++++++++++++++ ...24\354\235\264\353\237\254\354\212\244.py" | 53 +++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 "eclir/BOJ/SILVER/[1260]DFS\354\231\200BFS.py" create mode 100644 "eclir/BOJ/SILVER/[2606]\353\260\224\354\235\264\353\237\254\354\212\244.py" diff --git "a/eclir/BOJ/SILVER/[1260]DFS\354\231\200BFS.py" "b/eclir/BOJ/SILVER/[1260]DFS\354\231\200BFS.py" new file mode 100644 index 0000000..9ca264a --- /dev/null +++ "b/eclir/BOJ/SILVER/[1260]DFS\354\231\200BFS.py" @@ -0,0 +1,77 @@ +''' +{입력} + +첫째 줄: 정점의 개수, 간선의 개수, 시작 정점 번호 +나머지 : 간선이 연결하는 두 정점의 번호 + +input값으로 그래프를 만든 뒤 오름차순으로 정렬해주었다.(위의 규칙 참고) +''' +import sys +input = sys.stdin.readline + +total_node, node_pairs, start_node = map(int, input().split()) +graph = [[] for _ in range(total_node + 1)] #딕셔너리 내부에 딕셔너리를 만듦. +for _ in range(node_pairs): + n, m = map(int, input().split()) + graph[n].append(m) + graph[m].append(n) + graph[n].sort() + graph[m].sort() + + + +''' +{dfs와 bfs 함수 정의} + +두 함수에서 공통적으로 visited 리스트를 써서 노드를 재방문하지 않도록 했다. + +1. dfs +재귀로 구현하였다. +시작점(start_node)에서 한 번에 갈 수 있는 다른 노드들을 확인하는 for문을 돌렸다. +for문 안에서 if조건을 만족시 그 노드로 이동하고(dfs 호출) +호출한 dfs함수 안에서 다시 dfs함수를 호출하며. 갈 수 있는 끝까지 탐색한다. +탐색이 종료되면 상위의 for문이 돌아간다. + +2. bfs +시작점(start_node)에서 한 번에 갈 수 있는 다른 노드들을 확인하는 for문을 돌린다. +이때 이 경로가 queue에 다 쌓이면 for문이 종료된다.(이동하지 x) +그리고 queue 가장 아래에 쌓인 값으로 기준점이 이동하여 for문이 돌아가고 +그곳에서의 경로를 탐색한다. 이 값들도 queue에 쌓인다. +이 과정이 반복된다. +''' +def dfs(graph, start_node, visited): + visited[start_node] = True + print(start_node, end = ' ') #방문한 노드 출력 + + for i in graph[start_node]: + if not visited[i]: + dfs(graph, i, visited) + + +from collections import deque + +def bfs(graph, start_node): + visited[start_node] = True + queue = deque([start_node]) + + while queue: + node = queue.popleft() #가장 아래에 있는 값 빼내기 + print(node, end = ' ') + for i in graph[node]: + if not visited[i]: + queue.append(i) #queue에 값 넣기 + visited[i] = True + + + +''' +{메인} + +노드 방문 여부를 포함하는 리스트 visited를 초기화하고 +dfs와 bfs를 돌렸다. +''' +visited = [False] * (total_node + 1) #노드 개수가 8개라면 8번칸까지 필요하므로 0~8 즉, 9칸을 만듦. +dfs(graph, start_node, visited) +print() +visited = [False] * (total_node + 1) #visited 리스트 다시 초기화 +bfs(graph, start_node) \ No newline at end of file diff --git "a/eclir/BOJ/SILVER/[2606]\353\260\224\354\235\264\353\237\254\354\212\244.py" "b/eclir/BOJ/SILVER/[2606]\353\260\224\354\235\264\353\237\254\354\212\244.py" new file mode 100644 index 0000000..26d8dfe --- /dev/null +++ "b/eclir/BOJ/SILVER/[2606]\353\260\224\354\235\264\353\237\254\354\212\244.py" @@ -0,0 +1,53 @@ +''' +{입력} + +첫째 줄 : 컴퓨터의 수, 둘쨰 줄 : 직접 연결되어 있는 컴퓨터 쌍의 수 +나머지 줄: (둘째 줄 수만큼) 한 쌍씩 직접 연결되어 있는 컴퓨터 번호 + + +''' +import sys +input = sys.stdin.readline + +total_node = int(input()) +node_pairs = int(input()) +graph = [[] for _ in range(total_node + 1)] #딕셔너리 내부에 딕셔너리를 만듦 +for _ in range(node_pairs): + n, m = map(int, input().split()) + graph[n].append(m) + graph[m].append(n) + + + +''' +{dfs 함수(재귀) 정의} + +먼저 visited 리스트를 써서 노드를 재방문하지 않도록 했다. + +그리고 시작점(start_node)에서 한 번에 갈 수 있는 다른 노드들을 확인하는 for문을 돌렸다. +for문 안에서 if조건을 만족시 그 노드로 이동하고(dfs 호출) +호출한 dfs함수 안에서 다시 dfs함수를 호출하며. 갈 수 있는 끝까지 탐색한다. +탐색이 종료되면 상위의 for문이 돌아간다. + +리턴값은 방문한 노드의 정보를 담고 있는 visited 리스트. +''' +def dfs(graph, start_node, visited): + visited[start_node] = True + + for i in graph[start_node]: + if not visited[i]: + dfs(graph, i, visited) + return visited + + + +''' +{메인} + +노드 방문 여부를 포함하는 리스트 visited를 초기화하고 +dfs를 돌렸다. +dfs가 반환하는 visited 리스트에서 count함수로 +방문 노드의 개수를 추출하였다.(단, 첫번째 시작 노드는 제외) +''' +visited = [False] * (total_node + 1) +print(dfs(graph, 1, visited).count(True) - 1) \ No newline at end of file