Skip to content

Commit 5a2067f

Browse files
committed
2 parents 7501e8d + 4aaaf76 commit 5a2067f

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
input = sys.stdin.readline
3+
sys.setrecursionlimit(10**6) # 재귀 깊이 제한 설정(10만)
4+
5+
# 1. 입력 처리
6+
N, R, Q = map(int, input().split()) # 정점 수, 루트 번호, 쿼리 수
7+
graph = [[] for _ in range(N + 1)] # 트리 그래프
8+
9+
# 2. 트리 정보 입력
10+
for _ in range(N - 1):
11+
u, v = map(int, input().split())
12+
graph[u].append(v)
13+
graph[v].append(u)
14+
15+
# 3. 서브트리 크기 기록 배열
16+
size = [0] * (N + 1)
17+
18+
# 4. DFS를 이용한 서브트리 크기 계산
19+
def countSubtreeNodes(current):
20+
size[current] = 1 # 자기 자신 포함
21+
for node in graph[current]:
22+
if size[node] == 0: # 아직 방문하지 않은 경우
23+
countSubtreeNodes(node)
24+
size[current] += size[node] # 자식 서브트리 크기 추가
25+
26+
# 5. 루트에서 시작해 서브트리 크기 계산
27+
countSubtreeNodes(R)
28+
29+
# 6. 쿼리 처리
30+
for _ in range(Q):
31+
U = int(input())
32+
print(size[U])
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
input = sys.stdin.readline
3+
sys.setrecursionlimit(10 ** 6) # 재귀 깊이 설정 (10만)
4+
5+
# 1. 입력 받기
6+
N = int(input()) # 마을 개수
7+
people = [0] + list(map(int, input().split())) # 각 마을 주민 수 (인덱스 맞추기 위해 0 추가)
8+
9+
# 2. 마을 연결 (트리 구성)
10+
town = [[] for _ in range(N + 1)]
11+
12+
for _ in range(N - 1):
13+
u, v = map(int, input().split())
14+
town[u].append(v)
15+
town[v].append(u)
16+
17+
# 3. DP 테이블 초기화 및 방문 리스트
18+
dp = [[0, 0] for _ in range(N + 1)] # dp[i][0]: 우수 마을 X, dp[i][1]: 우수 마을 O
19+
visited = [False] * (N + 1)
20+
21+
22+
# 4. DFS를 통한 서브트리 탐색 및 DP 계산
23+
def dfs(current):
24+
visited[current] = True # 현재 노드 방문 처리
25+
dp[current][1] += people[current] # 현재 마을을 우수 마을로 선정한 경우 (자기 자신 포함)
26+
27+
for child in town[current]:
28+
if not visited[child]:
29+
dfs(child) # 자식 노드로 DFS 진행
30+
dp[current][1] += dp[child][0] # 자식 노드가 우수 마을이 아닌 경우 주민 수 더하기
31+
dp[current][0] += max(dp[child][0], dp[child][1]) # 자식 노드에서 우수 마을이거나 아닌 경우 중 최대값 선택
32+
33+
34+
# 5. DFS 호출 (루트 노드 1부터 시작)
35+
dfs(1)
36+
37+
# 6. 결과 출력
38+
print(max(dp[1][0], dp[1][1]))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
n = int(input()) # 색종이의 수
5+
paper = [[0] * 100 for _ in range(100)] # 100x100 도화지 초기화
6+
7+
# 색종이를 하나씩 붙이기
8+
for _ in range(n):
9+
x, y = map(int, input().split())
10+
11+
# 10x10 크기의 영역을 1로 채운다.
12+
for i in range(x, x + 10):
13+
for j in range(y, y + 10):
14+
paper[i][j] = 1
15+
16+
# 도화지에서 1의 개수를 모두 더하여 넓이 계산
17+
answer = 0
18+
for row in paper:
19+
answer += sum(row)
20+
21+
print(answer)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 1. 과목 평점 테이블 (딕셔너리 사용)
5+
rating = {
6+
"A+": 4.5, "A0": 4.0,
7+
"B+": 3.5, "B0": 3.0,
8+
"C+": 2.5, "C0": 2.0,
9+
"D+": 1.5, "D0": 1.0,
10+
"F": 0.0
11+
}
12+
13+
# 2. 변수 초기화
14+
total_score = 0 # 총 평점 (학점 * 과목 평점의 합)
15+
total_credit = 0 # 총 학점
16+
17+
# 3. 과목별 입력 및 평점 계산
18+
for _ in range(20):
19+
subject, credit, grade = input().split()
20+
credit = float(credit) # 학점은 소수점으로 변환
21+
22+
if grade != 'P': # P 등급 과목 제외
23+
total_score += credit * rating[grade]
24+
total_credit += credit
25+
26+
# 4. 전공 평점 계산 및 출력
27+
GPA = total_score / total_credit
28+
print("{:.6f}".format(GPA)) # 소수점 6자리 출력
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
7+
def AC():
8+
p = input().strip() # 수행할 함수
9+
n = int(input()) # 배열 크기
10+
nums = input().strip() # 배열 입력
11+
12+
# 배열 처리
13+
if nums == "[]":
14+
nums = deque()
15+
else:
16+
nums = deque(map(int, nums[1:-1].split(",")))
17+
18+
reverse = False # 뒤집기 플래그
19+
for command in p:
20+
if command == "R":
21+
reverse = not reverse # R이 나올 때마다 뒤집기 상태 토글
22+
elif command == "D":
23+
if not nums: # 비어있는 경우 에러 출력
24+
print("error")
25+
return
26+
if reverse: # 뒤집힌 상태라면 뒤에서 제거
27+
nums.pop()
28+
else: # 그렇지 않다면 앞에서 제거
29+
nums.popleft()
30+
31+
# 최종 출력
32+
if reverse:
33+
nums.reverse() # 필요시 뒤집기
34+
print("[" + ",".join(map(str, nums)) + "]")
35+
36+
37+
# 테스트 케이스 처리
38+
T = int(input()) # 테스트 케이스 수
39+
for _ in range(T):
40+
AC()

0 commit comments

Comments
 (0)