Post

🦊 μž‘μ—…

1. 문제 링크

2056번: μž‘μ—…


2. μ½”λ“œ

Pypy3 136904KB 632ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
N = int(input())

workflow = [] # λ‹€μŒλ…Έλ“œ
pre_workflow = [] # μ„ ν–‰λ…Έλ“œ
in_degree = [0] * N # μ§„μž…μ°¨μˆ˜
visit = [0] * N # λ°©λ¬Έ
time = []
for i in range(N):
    workflow.append([])
    pre_workflow.append([])

for i in range(N):
    work = list(map(int, input().split()))

    # κ±Έλ¦¬λŠ” μ‹œκ°„
    time.append(work[0])
    # μ„ ν–‰ μž‘μ—… μ—°κ²°
    for j in range(work[1]):
        pre_work = work[2 + j]
        # λ‹€μŒμ— μ§„ν–‰ν•΄μ•Όν•˜λŠ” κ°„μ„  μ—°κ²°
        workflow[pre_work - 1].append(i)
        in_degree[i] += 1
        # μ„ ν–‰ μž‘μ—…μœΌλ‘œλΆ€ν„° λ“€μ–΄μ˜€λŠ” κ°„μ„  μ—°κ²°
        pre_workflow[i].append(pre_work - 1)

# ν˜„μž¬ μž‘μ—…κΉŒμ§€ μ†Œμš”λœ 총 μ‹œκ°„
dp = [0] * 10001

# μœ„μƒμ •λ ¬ 큐
q = [0]
visit[0] = 1
total_time = 0
while q:
    cur = q.pop(0)

    # μ§„μž… λ…Έλ“œλ“€μ˜ μ‹œκ°„μ„ 기반으둜 ν˜„μž¬ μ‹œκ°„μ„ 지정
    pre_time = 0
    for i in range(len(pre_workflow[cur])):
        pre_time = max(pre_time, dp[pre_workflow[cur][i]])
    dp[cur] = pre_time + time[cur]
    total_time = max(total_time, dp[cur])

    # λ‹€μŒ λ…Έλ“œμ˜ μ§„μž… 차수λ₯Ό 제거
    for i in range(len(workflow[cur])):
        node = workflow[cur][i]
        in_degree[node] -= 1

    # μ§„μž… μ°¨μˆ˜κ°€ 0인 λ…Έλ“œ 큐에 μ‚½μž…
    for i in range(N):
        if not visit[i] and in_degree[i] == 0:
            q.append(i)
            visit[i] = 1

print(total_time)


3. ν•΄μ„€

λͺ¨λ“  λ…Έλ“œμ˜ μ§„μž…λ…Έλ“œ, μ§„μΆœλ…Έλ“œλ₯Ό κΈ°λ‘ν•˜μ—¬ μœ„μƒμ •λ ¬ μˆ˜ν–‰ μœ„μƒ μ •λ ¬ κ³Όμ •μ—μ„œ μ§„μž… λ…Έλ“œλ“€μ˜ μ΅œλŒ“κ°’μ„ 가져와 μ„ ν–‰ μž‘μ—…μ΄ λͺ¨λ‘ λλ‚˜λŠ” μ‹œκ°„ κ°€μ Έμ˜΄ κ·Έ μ‹œκ°„ + ν˜„μž¬ μž‘μ—…μ˜ μ‹œκ°„μœΌλ‘œ κ±Έλ¦¬λŠ” μ‹œκ°„μ˜ 합을 ꡬ함

This post is licensed under CC BY 4.0 by the author.