목록전체 글 (108)
1seul357
def Find(x): if P[x] != x: P[x] = Find(P[x]) return P[x] def Union(a, b): pa = Find(a) pb = Find(b) P[pb] = pa T = int(input()) for TC in range(T): N, M = map(int, input().split()) P = [i for i in range(N+1)] edges = [] ans = 0 for i in range(M): n1, n2, w = map(int, input().split()) edges.append((w, n1, n2)) edges.sort() for w, n1, n2 in edges: if Find(n1) == Find(n2): continue Union(n1, n2) an..
T = int(input()) for TC in range(T): N = int(input()) MAP = [list(map(int, input().split())) for _ in range(N)] check = [[10000] * N for _ in range(N)] check[0][0] = 0 q = [] q.append((0, 0)) dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] while len(q) != 0: now_dr, now_dc = q.pop(0) for i in range(4): next_dr = now_dr + dr[i] next_dc = now_dc + dc[i] if 0 > next_dr or next_dr >= N or 0 > next_dc or next_..
def Find(x): if P[x] != x: P[x] = Find(P[x]) return P[x] def Union(a, b): pa = Find(a) pb = Find(b) P[pb] = pa T = int(input()) for TC in range(T): N, M = map(int, input().split()) P = [i for i in range(N + 1)] edges = [] counts = [0] * (N+1) ans = 0 for i in range(M): n1, n2= map(int, input().split()) edges.append((n1, n2)) for i in range(2): for n1, n2 in edges: if Find(n1) == Find(n2): contin..
def bfs(): # BFS 탐색 dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] while len(q) != 0: # q에 값이 있을 때까지 반복 x, y = q.pop(0) # 값 꺼내기 for i in range(4): # 상, 하, 좌, 우 탐색 now_dr = x + dr[i] now_dc = y + dc[i] if 0
def dfs(idx): global count if idx == N: count += 1 return for i in range(N): if visited[i] == 0: visited[i] = 1 dfs(idx+1) visited[i] = 0 T = 10 for TC in range(T): N = int(input()) visited = [0]*N count = 0 dfs(0) print('#{} {}'.format(TC+1, count))
def dfs(row, col, cnt, ans): dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] if cnt == 7: answer.append(ans) return for i in range(4): now_dr = row + dr[i] now_dc = col + dc[i] if 0
def dfs(now, ans): global min_height if ans >= B: # 키가 B이상이고, if min_height > ans: # 최소키보다 ans가 작으면 min_height = ans # 갱신 return if now > min_height: # 가지치기. 가망 없으면 리턴 return for i in range(now, N): if visited[i] == 0: # 중복체크. 아직 안쓴 키면 탐색하기 visited[i] = 1 dfs(i, ans + Height[i]) visited[i] = 0 T = int(input()) for TC in range(T): N, B = map(int, input().split()) Height = list(map(int, input().sp..