본문 바로가기

분류 전체보기

(402)
[백준 1774번] 우주신과의 교감 (Python3) import sys input = sys.stdin.readline from heapq import heappush,heappop from math import sqrt N,M = map(int,input().split()) co = [] for i in range(N): co.append([*map(int,input().split())]) graph = [[0]*N for i in range(N)] for i in range(N-1): x1,y1 = co[i] for j in range(i+1,N): x2,y2 = co[j] d = sqrt((x1-x2)**2+(y1-y2)**2) graph[i][j] = graph[j][i] = d for i in range(M): x,y = map(lambda x:..
[백준 11693번] n^m의 약수의 합 (Python3) import sys input = sys.stdin.readline from math import sqrt mod = 1000000007 def multiply(x,n): if n == 1: return x xn2 = multiply(x,n//2) if n%2: return xn2*xn2*x %mod else: return xn2*xn2 %mod N,M = map(int,input().split()) N1 = N prime = {} for p in range(2,int(sqrt(N))+1): if N1%p: continue prime[p] = 0 while N1%p == 0: N1 //= p prime[p] += 1 if N1 != 1: prime[N1] = 1 numer = 1 denom = 1 for..
[백준 17435번] 합성함수와 쿼리 (Python3) 처음에는 union-find 문제라고 생각했다. union-find를 이용해서 cycle과 cycle이 아닌 것을 찾고, cycle은 cycle의 크기를 기록, cycle이 아니면 cycle까지의 거리를 저장하고, 주어지는 n에 대해서 cycle의 크기로 나눈 나머지만큼 함수를 실행하면 된다고 생각했다. 코드는 다음과 같다. import sys input = sys.stdin.readline def findparent(x): if parent[x] == x: return x parent[x] = findparent(parent[x]) return parent[x] def DFS(x): if visited[x]: cyclecount(x) return visited[x] = 1 DFS(f[x]) if deg..
[백준 16496번] 큰 수 만들기 (Python3) import sys input = sys.stdin.readline N = int(input()) data = [*input().split()] maxL = 0 for d in data: maxL = max(maxL,len(d)) maxL *= 2 nums = [] for d in data: newd = d*(maxL//len(d)) for i in range(maxL%len(d)): newd += d[i] nums.append((newd,len(d))) nums.sort(reverse = True) result = "" for n,l in nums: for i in range(l): result += n[i] if result[0]=="0": print(0) else: print(result) 알고리즘..
[백준 21276번] 계보 복원가 호석 (Python3) import sys input = sys.stdin.readline from collections import deque N = int(input()) name = sorted(input().split()) M = int(input()) child = {i:set() for i in name} realchild = {i:set() for i in name} indegree = {i:0 for i in name} for i in range(M): c,p = input().split() child[p].add(c) indegree[c] += 1 father = [] dq = deque() for i in name: #시조 if not indegree[i]: father.append(i) dq.append(i..
[백준 21609번] 상어 중학교 (Python3) import sys input = sys.stdin.readline from collections import deque dy = [1,-1,0,0] dx = [0,0,1,-1] def BFS(y,x,color): dq = deque() dq.append((y,x)) result = set() rainbow = set() while dq: y,x = dq.popleft() if visited[y][x]: continue visited[y][x] = 1 if matrix[y][x] == -2: rainbow.add((y,x)) result.add((y,x)) for i in range(4): y1,x1 = y+dy[i],x+dx[i] if N>y1>=0 and N>x1>=0: if visited[y1][x..
[백준 1014번] 컨닝 (Python3) import sys input = sys.stdin.readline def DFS(n,bit,cnt): if DP[n][bit]: return if n == N-1: DP[n][bit] = cnt return for bit1,cnt1 in bitlist[n+1]: if bit&(bit1
[백준 14939번] 불 끄기 (Python3) import sys input = sys.stdin.readline dy = [0,1,0,-1,0] dx = [0,0,1,0,-1] def switch(y,x): #5방향 켜고끄기 for i in range(5): y1,x1 = y+dy[i],x+dx[i] if 10>y1>=0 and 10>x1>=0: board[y1][x1] = abs(board[y1][x1]-1) board = [] for i in range(10): board.append([*map(lambda x:1 if x=="O" else 0,[*input().strip()])]) result = -1 def DFS(y,x,cnt): global result if result != -1: #결과가 나왔으면 종료 return if x == 10..