結果

問題 No.1477 Lamps on Graph
ユーザー FromBooska
提出日時 2023-04-22 10:13:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 1,460 bytes
コンパイル時間 1,317 ms
コンパイル使用メモリ 81,888 KB
実行使用メモリ 110,420 KB
最終ジャッジ日時 2024-11-07 00:04:48
合計ジャッジ時間 10,158 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

# Ax<Ayのときだけ有向辺を張る
# ということは入次数が0のところがある、そこからdequeできる
# current = que.popleft()して、ランプ点灯ならスイッチしてans.append()
# dequeが終わって全部消えているか

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[v] < A[u]:
        edges[v].append(u)
        inward[u] += 1

K = int(input())
B = list(map(int, input().split()))
B_list = [0]*(N+1)

for b in B:
    B_list[b] = 1

#print(edges)
#print(inward)
#print(B)
#print(B_list)

from collections import deque
que = deque()
for i in range(1, N+1):
    if inward[i] == 0:
        que.append(i)
        
#print('que', que)

ans_list = []
while que:
    current = que.popleft()
    
    if B_list[current] == 1:
        B_list[current] ^= 1
        ans_list.append(current)
        for nxt in edges[current]:
            inward[nxt] -= 1
            B_list[nxt] ^= 1
            if inward[nxt] == 0:
                que.append(nxt)
    else:
        for nxt in edges[current]:
            inward[nxt] -= 1
            if inward[nxt] == 0:
                que.append(nxt)        
            
if sum(B_list) == 0:
    print(len(ans_list))
    for a in ans_list:
        print(a)
else:
    print(-1)



0