結果
問題 | No.1477 Lamps on Graph |
ユーザー | terasa |
提出日時 | 2022-06-08 00:21:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 313 ms / 2,000 ms |
コード長 | 2,117 bytes |
コンパイル時間 | 255 ms |
コンパイル使用メモリ | 82,228 KB |
実行使用メモリ | 114,804 KB |
最終ジャッジ日時 | 2024-09-21 05:00:45 |
合計ジャッジ時間 | 11,381 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
ソースコード
import sys import pypyjit import itertools import heapq import math from collections import deque, defaultdict import bisect input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) pypyjit.set_param('max_unroll_recursion=-1') def index_lt(a, x): 'return largest index s.t. A[i] < x or -1 if it does not exist' return bisect.bisect_left(a, x) - 1 def index_le(a, x): 'return largest index s.t. A[i] <= x or -1 if it does not exist' return bisect.bisect_right(a, x) - 1 def index_gt(a, x): 'return smallest index s.t. A[i] > x or len(a) if it does not exist' return bisect.bisect_right(a, x) def index_ge(a, x): 'return smallest index s.t. A[i] >= x or len(a) if it does not exist' return bisect.bisect_left(a, x) class TopologicalSort(): def __init__(self, N, E, D): self.N = N self.E = E self.D = D def sort(self): dq = deque([]) for i in range(self.N): if self.D[i] == 0: dq.append(i) if len(dq) == 0: return False, None ret = [] while len(dq) > 0: v = dq.popleft() ret.append(v) for dest in self.E[v]: self.D[dest] -= 1 if self.D[dest] == 0: dq.append(dest) for i in range(self.N): if not self.D[i] == 0: return False, None return True, ret N, M = map(int, input().split()) A = list(map(int, input().split())) E = [[] for _ in range(N)] D = [0] * N for _ in range(M): u, v = map(int, input().split()) u -= 1 v -= 1 if A[u] == A[v]: continue if A[u] > A[v]: u, v = v, u E[u].append(v) D[v] += 1 K = int(input()) B = list(map(lambda x: int(x) - 1, input().split())) lamp = [False] * (N) for b in B: lamp[b] = True success, V = TopologicalSort(N, E, D).sort() assert success is True ans = [] for v in V: if lamp[v] is True: lamp[v] = False ans.append(v + 1) for d in E[v]: lamp[d] = not lamp[d] print(len(ans)) print(*ans, sep='\n')