Skip to content

Commit b8ae06a

Browse files
authored
Merge pull request #117 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 1์›” 1์ฃผ์ฐจ / 5๋ฌธ์ œ
2 parents 1302ca6 + 0d434d6 commit b8ae06a

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)