結果

問題 No.1477 Lamps on Graph
ユーザー tobusakana
提出日時 2022-11-27 14:42:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 1,052 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 82,068 KB
実行使用メモリ 115,248 KB
最終ジャッジ日時 2024-10-04 02:57:31
合計ジャッジ時間 8,520 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline = sys.stdin.readline
N,M = map(int,readline().split())
A = list(map(int,readline().split()))
G = [[] for i in range(N)]
indegree = [0] * N
for _ in range(M):
    u,v = map(int,readline().split())
    u -= 1
    v -= 1
    if A[u] < A[v]:
        G[u].append(v)
        indegree[v] += 1
    if A[u] > A[v]:
        G[v].append(u)
        indegree[u] += 1
        
starts = []
for i in range(N):
    if indegree[i] == 0:
        starts.append(i)
        
K = int(readline())
B = list(map(int,readline().split()))
lamp_on = [False] * N
for b in B:
    lamp_on[b - 1] = True

ans = []    
while starts:
    next_starts = []
    for s in starts:
        if lamp_on[s]:
            lamp_on[s] = False
            ans.append(s)
            for child in G[s]:
                lamp_on[child] ^= True
        for child in G[s]:
            indegree[child] -= 1
            if indegree[child] == 0:
                next_starts.append(child)
    starts = next_starts
    
print(len(ans))
for a in ans:
    print(a + 1)
            
            
0