백준 40

백준[Python] 2667.단지번호붙이기 - 파이썬

📖 문제 📃 코드 import sys input = sys.stdin.readline N = int(input()) complex = [] result = [] cnt = 0 # 아래, 위, 왼쪽, 오른쪽 dx = [0, 0, -1, 1] dy = [1, -1, 0, 0] def bfs(x, y): global cnt if x = N or y = N: return if complex[x][y] == 1: cnt += 1 # 다시 방문하지 않게 초기화 complex[x][y] = 0 for i in range(4): nx = x + dx[i] ny = y + dy[i] bfs(nx, ny) for i in range(N): complex.append(list(map(in..

백준[Python] 13023.ABCDE - 파이썬

📖 문제 📃 코드 import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline N, M = map(int, input().split()) relation = [[] for i in range(N)] visited = [False] * (N+1) ans = False for i in range(M): a, b = map(int, input().split()) relation[a].append(b) relation[b].append(a) def dfs(idx, depth): global ans visited[idx] = True # 1. 종료조건 # case A-B-C-D-E 같은 친구 관계가 있는지 검사 if depth == 4: ans = Tru..

백준[Python] 11724.연결 요소의 개수 - 파이썬

📖 문제 📃 코드 import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline N, M = map(int, input().split()) matrix = [[0]*(N+1) for i in range(N+1)] visited = [False] * (N+1) for i in range(M): a, b = map(int, input().split()) matrix[a][b] = matrix[b][a] = 1 def dfs(v): visited[v] = True for node in range(len(matrix[v])): if visited[node] == False and matrix[v][node] == 1: dfs(node) cnt = 0 fo..

백준[Python] 1260.DFS와 BFS - 파이썬

📖 문제 📃 코드 from collections import deque import sys input = sys.stdin.readline N, M, V = map(int, input().split()) matrix = [[0]*(N+1) for i in range(N+1)] visited = [False] * (N+1) visited1 = [False] * (N+1) for i in range(M): a, b = map(int, input().split()) matrix[a][b] = matrix[b][a] = 1 def dfs(V): visited[V] = True print(V, end=" ") # 수행동작 for i in range(N+1): if visited[i] == False and mat..

백준[Python] 14425.문자열 집합 - 파이썬

📖 문제 📃 코드 import sys import collections input = sys.stdin.readline N, M = map(int, input().split()) strHash = collections.defaultdict(int) cnt = 0 for _ in range(N): s = input().strip() strHash[s] for _ in range(M): s = input().strip() if s in strHash: cnt += 1 print(cnt) 🔗 링크 https://www.acmicpc.net/problem/14425 14425번: 문자열 집합 첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다. 다음 N개의 줄..

백준[Python] 10815.숫자 카드 - 파이썬

📖 문제 📃 코드 import sys n = int(input()) card = list(map(int, sys.stdin.readline().split())) m = int(input()) check = list(map(int, sys.stdin.readline().split())) card.sort() def binarySearch(array, key, start, end): while start key: end = mid-1 elif array[mid] < key: start = mid+1 return None for i in range(m): if binarySearch(card, check[i], 0, n-1) is not None: print(1, end=' ') else: print(0, e..

백준[JAVA] 11279.최대 힙 - 자바

📖 문제 📃 코드 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collections; import java.util.PriorityQueue; public class Main { static PriorityQueue heap = new PriorityQueue(Collections.reverseOrder()); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int ..