結果

問題 No.1477 Lamps on Graph
ユーザー 👑 KazunKazun
提出日時 2021-04-16 21:22:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 5,549 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 168,892 KB
最終ジャッジ日時 2024-07-03 00:40:04
合計ジャッジ時間 11,677 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

class Digraph:
    """重み[なし]有向グラフを生成する.

    """

    #入力定義
    def __init__(self,vertex=[]):
        self.vertex=set(vertex)

        self.edge_number=0
        self.vertex_number=len(vertex)

        self.adjacent_out={v:set() for v in vertex} #出近傍(vが始点)
        self.adjacent_in={v:set() for v in vertex} #入近傍(vが終点)

    #頂点の追加
    def add_vertex(self,*adder):
        for v in adder:
            if not self.vertex_exist(v):
                self.adjacent_in[v]=set()
                self.adjacent_out[v]=set()

                self.vertex.add(v)
                self.vertex_number+=1

    #辺の追加
    def add_edge(self,From,To):
        self.add_vertex(From)
        self.add_vertex(To)

        if To not in self.adjacent_out[From]:
            self.adjacent_out[From].add(To)
            self.adjacent_in[To].add(From)
            self.edge_number+=1

    #辺を除く
    def remove_edge(self,From,To):
        self.add_vertex(From)
        self.add_vertex(To)

        if To in self.adjacent_out[From]:
            self.adjacent_out[From].discard(To)
            self.adjacent_in[To].discard(From)
            self.edge_number-=1

    #頂点を除く
    def remove_vertex(self,*vertexes):
        for  v in vertexes:
            if v in self.vertex:
                self.vertex_number-=1

                for u in self.adjacent_out[v]:
                    self.adjacent_in[u].discard(v)
                    self.edge_number-=1
                del self.adjacent_out[v]

                for u in self.adjacent_in[v]:
                    self.adjacent_out[u].discard(v)
                    self.edge_number-=1
                del self.adjacent_in[v]
                self.vertex.discard(v)

    #Walkの追加
    def add_walk(self,*walk):
        N=len(walk)
        for k in range(N-1):
            self.add_edge(walk[k],walk[k+1])

    #Cycleの追加
    def add_cycle(self,*cycle):
        self.add_walk(*cycle)
        self.add_edge(cycle[-1],cycle[0])

    #頂点の交換
    def __vertex_swap(self,p,q):
        self.vertex.sort()

    #グラフに頂点が存在するか否か
    def vertex_exist(self,v):
        return v in self.vertex

    #グラフに辺が存在するか否か
    def edge_exist(self,From,To):
        if self.vertex_exist(From) and self.vertex_exist(To):
            return False
        return To in self.adjacent_out[From]

    #近傍
    def neighbohood(self,v):
        """vの出近傍, 入近傍を出力する.

        Input:
        v:頂点

        Output:
        (出近傍, 入近傍)
        """
        if not self.vertex_exist(v):
            return (set(),set())
        return (self.adjacent_out[v],self.adjacent_in[v])

    #出次数
    def out_degree(self,v):
        if not self.vertex_exist(v):
            return 0
        return len(self.adjacent_out[v])

    #入次数
    def in_degree(self,v):
        if not self.vertex_exist(v):
            return 0
        return len(self.adjacent_in[v])

    #次数
    def degree(self,v):
        return (self.out_degree(v),self.in_degree(v))

    #頂点数
    def vertex_count(self):
        return self.vertex_number

    #辺数
    def edge_count(self):
        return self.edge_number

    #頂点vに到達可能な頂点
    def reachable_to(self,v):
        if not self.vertex_exist(v):
            return []

        from collections import deque
        T={v:0 for v in self.vertex}
        T[v]=1
        Q=deque([v])
        while Q:
            x=Q.popleft()
            for y in self.adjacent_in[x]:
                if not T[y]:
                    T[y]=1
                    Q.append(y)
        return [x for x in self.vertex if T[x]]

    #頂点vから到達可能な頂点
    def reachable_from(self,v):
        if not self.vertex_exist(v):
            return []

        from collections import deque
        T={v:0 for v in self.vertex}
        T[v]=1
        Q=deque([v])
        while Q:
            x=Q.popleft()
            for y in self.adjacent_out[x]:
                if not T[y]:
                    T[y]=1
                    Q.append(y)
        return [x for x in self.vertex if T[x]]

    #深いコピー
    def deepcopy(self):
        from copy import deepcopy
        D=Digraph()
        D.vertex=deepcopy(self.vertex)
        D.edge_number=self.edge_number
        D.vertex_number=len(self.vertex)
        D.adjacent_out=deepcopy(self.adjacent_out)
        D.adjacent_in=deepcopy(self.adjacent_in)
        return D

#Topologycal Sort
def Topological_Sort(D):
    from collections import deque

    X={v:D.in_degree(v) for v in D.vertex}
    Q=deque([v for v in D.vertex if X[v]==0])

    S=[]
    while Q:
        u=Q.pop()
        S.append(u)
        for v in D.adjacent_out[u]:
            X[v]-=1
            if X[v]==0:
                Q.append(v)
    return S
#================================================
import sys
from collections import deque
input=sys.stdin.readline
write=sys.stdout.write

N,M=map(int,input().split())
A=["*"]+list(map(int,input().split()))

D=Digraph(list(range(1,N+1)))
for _ in range(M):
    u,v=map(int,input().split())

    if A[u]<A[v]:
        D.add_edge(u,v)
    elif A[u]>A[v]:
        D.add_edge(v,u)

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

for b in B:
    Lamp[b]=1

T=Topological_Sort(D)

Z=[]
for x in T:
    if Lamp[x]==1:
        Z.append(x)
        for y in D.adjacent_out[x]:
            Lamp[y]^=1

print(len(Z))
write("\n".join(map(str,Z)))
0