結果

問題 No.1477 Lamps on Graph
ユーザー FromBooskaFromBooska
提出日時 2023-03-09 22:08:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 314 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 115,740 KB
最終ジャッジ日時 2023-10-18 06:29:31
合計ジャッジ時間 9,457 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
55,500 KB
testcase_01 AC 43 ms
55,500 KB
testcase_02 AC 43 ms
55,500 KB
testcase_03 AC 43 ms
55,500 KB
testcase_04 AC 43 ms
55,500 KB
testcase_05 AC 43 ms
55,500 KB
testcase_06 AC 43 ms
55,500 KB
testcase_07 AC 42 ms
55,500 KB
testcase_08 AC 43 ms
55,500 KB
testcase_09 AC 43 ms
55,500 KB
testcase_10 AC 43 ms
55,500 KB
testcase_11 AC 43 ms
55,500 KB
testcase_12 AC 233 ms
85,128 KB
testcase_13 AC 213 ms
88,744 KB
testcase_14 AC 256 ms
86,548 KB
testcase_15 AC 171 ms
79,660 KB
testcase_16 AC 148 ms
82,108 KB
testcase_17 AC 146 ms
81,940 KB
testcase_18 AC 186 ms
93,332 KB
testcase_19 AC 210 ms
87,336 KB
testcase_20 AC 148 ms
78,816 KB
testcase_21 AC 176 ms
91,524 KB
testcase_22 AC 128 ms
77,464 KB
testcase_23 AC 187 ms
80,044 KB
testcase_24 AC 268 ms
94,588 KB
testcase_25 AC 163 ms
79,156 KB
testcase_26 AC 253 ms
95,224 KB
testcase_27 AC 179 ms
81,036 KB
testcase_28 AC 205 ms
84,780 KB
testcase_29 AC 195 ms
81,656 KB
testcase_30 AC 129 ms
89,804 KB
testcase_31 AC 145 ms
82,784 KB
testcase_32 AC 225 ms
115,740 KB
testcase_33 AC 222 ms
111,456 KB
testcase_34 AC 195 ms
109,556 KB
testcase_35 AC 314 ms
98,208 KB
testcase_36 AC 306 ms
95,984 KB
testcase_37 AC 298 ms
95,292 KB
testcase_38 AC 298 ms
95,292 KB
testcase_39 AC 313 ms
96,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 整数の大小関係で有効グラフになる
# 各連結成分ごとに調べる、そのために入次数0を記録
# 連結成分でON=[0101]を調べる、連結成分の始めから見ていく
# switch使ったら記録
# BFS or DFSだろう
# 問題読み違えていた、ランプは次だけ変わる

N, M = map(int, input().split())
A = [0]+list(map(int, input().split()))
edges = [[] for i in range(N+1)]
inward = [0]*(N+1)
for i in range(M):
    u, v = map(int, input().split())
    if A[u] < A[v]:
        edges[u].append(v)
        inward[v] += 1
    elif A[u] > A[v]:
        edges[v].append(u)
        inward[u] += 1

K = int(input())
B = list(map(int, input().split()))


# その連結成分の始めからやらないと最小手順にならない
# ということは連結成分の最初を探す必要がある, degree check
# 問題読み違えていた、ランプは次だけ変わる

from collections import deque

onoff = [0]*(N+1)
for b in B:
    onoff[b] = 1
    
start = []
for i in range(1, N+1):
    if inward[i] == 0:
        start.append(i)

switch_list = []

ans = []    
while start:
    next_start = []
    for s in start:
        if onoff[s] == 1:
            # スイッチ押す
            onoff[s] = 0
            ans.append(s)
            for nxt in edges[s]:
                onoff[nxt] ^= 1
        for nxt in edges[s]:
            inward[nxt] -= 1
            # 入次数がゼロになれば、その頂点以前はすべて処理済みということ
            if inward[nxt] == 0:
                next_start.append(nxt)
    start = next_start
    
print(len(ans))
for a in ans:
    print(a)
0