結果

問題 No.762 PDCAパス
ユーザー kept1994kept1994
提出日時 2021-09-02 22:22:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,821 bytes
コンパイル時間 1,977 ms
コンパイル使用メモリ 86,600 KB
実行使用メモリ 84,172 KB
最終ジャッジ日時 2023-08-20 11:21:21
合計ジャッジ時間 8,540 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
75,768 KB
testcase_01 AC 97 ms
71,196 KB
testcase_02 AC 94 ms
71,180 KB
testcase_03 AC 93 ms
71,360 KB
testcase_04 AC 95 ms
71,268 KB
testcase_05 AC 81 ms
71,268 KB
testcase_06 AC 77 ms
71,460 KB
testcase_07 AC 76 ms
71,352 KB
testcase_08 AC 76 ms
71,464 KB
testcase_09 AC 74 ms
71,496 KB
testcase_10 AC 74 ms
71,392 KB
testcase_11 AC 76 ms
71,348 KB
testcase_12 AC 78 ms
71,308 KB
testcase_13 AC 83 ms
71,192 KB
testcase_14 AC 78 ms
71,192 KB
testcase_15 AC 96 ms
71,388 KB
testcase_16 AC 99 ms
75,192 KB
testcase_17 AC 93 ms
71,460 KB
testcase_18 AC 95 ms
71,420 KB
testcase_19 AC 96 ms
71,400 KB
testcase_20 AC 95 ms
71,460 KB
testcase_21 AC 92 ms
71,300 KB
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10 ** 9)

def main():
    N, M = map(int, input().split())
    S = input()
    pdca = "PDCA"
    ans = 0
    A = [pdca.index(ss) for ss in S]
    class DFS():
        def __init__(self, N: int) -> None:
            self.nodes = N                          # 頂点数
            self.G = [[] for _ in range(N)]         # グラフ
            self.seen = [False] * N                 # 各ノードが訪問済みかどうかのフラグ
            self.firstOrder = []                    # ノードの行きがけ順(0-index)
            self.lastOrder = []                     # ノードの帰りがけ順(0-index)

        # 辺の追加
        def addEdge(self, fromNode: int, toNode: int, bothDirection: bool):
            self.G[fromNode].append(toNode)
            if bothDirection:
                self.G[toNode].append(fromNode)
        # DFS
        def build(self, now: int, pre:int):
            nonlocal ans
            # ----- ノードに到着した時の処理
            # self.firstOrder.append(now)
            # self.seen[now] = True
            # ----- 隣接する各ノードへの移動処理
            for next in self.G[now]:
                # if self.seen[next]:
                if A[next] != pre + 1:
                    continue
                if pre + 1 == 3:
                    ans += 1
                self.build(next, pre + 1)
            # ----- ノードから戻る時の処理
            # self.lastOrder.append(now)
            return

    d = DFS(N)
    for _ in range(M):
        u, v = map(lambda i : int(i) - 1, input().split())
        d.addEdge(v, u, bothDirection=True)
    
    for i in range(N):
        if A[i] == 0:
            d.build(i, 0)
    print(ans)
    return

if __name__ == '__main__':
    main()
0