結果

問題 No.762 PDCAパス
ユーザー nanaenanae
提出日時 2018-12-11 09:57:05
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,710 bytes
コンパイル時間 724 ms
コンパイル使用メモリ 97,108 KB
実行使用メモリ 9,784 KB
最終ジャッジ日時 2023-09-03 21:33:33
合計ジャッジ時間 3,761 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 35 ms
4,376 KB
testcase_23 AC 35 ms
4,376 KB
testcase_24 AC 64 ms
9,784 KB
testcase_25 AC 63 ms
8,748 KB
testcase_26 AC 46 ms
4,380 KB
testcase_27 AC 46 ms
4,380 KB
testcase_28 AC 60 ms
6,916 KB
testcase_29 AC 60 ms
7,740 KB
testcase_30 AC 56 ms
6,412 KB
testcase_31 AC 57 ms
5,952 KB
testcase_32 AC 56 ms
6,144 KB
testcase_33 AC 52 ms
5,200 KB
testcase_34 AC 52 ms
5,252 KB
testcase_35 AC 53 ms
6,460 KB
testcase_36 AC 46 ms
4,480 KB
testcase_37 AC 46 ms
4,392 KB
testcase_38 AC 47 ms
4,384 KB
testcase_39 AC 47 ms
4,388 KB
testcase_40 AC 46 ms
4,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum mod = 10^^9 + 7;

void main() {
    int[char] cnv = ['P':0, 'D':1, 'C':2, 'A':3];
    int n, m;
    scan(n, m);
    auto s = readln.chomp.map!(ch => cnv[ch.to!char]).array;
    auto adj = new int[][](n, 0);

    foreach (i ; 0 .. m) {
        int ui, vi;
        scan(ui, vi);
        ui--, vi--;
        if (s[ui] > s[vi]) {
            swap(ui, vi);
        }
        if (s[vi] - s[ui] == 1) {
            adj[vi] ~= ui;
        }
    }

    auto dp = new int[](n);
    dp[] = -1;

    int dfs(int v) {
        if (s[v] == 0) {
            return dp[v] = 1;
        }
        if (dp[v] != -1) {
            return dp[v];
        }
        dp[v] = 0;
        foreach (u ; adj[v]) {
            dp[v] += dfs(u);
            dp[v] %= mod;
        }
        return dp[v];
    }

    int ans;

    foreach (i ; 0 .. n) {
        if (dp[i] == -1) {
            dfs(i);
        }
        if (s[i] == 3) {
            ans += dp[i];
            ans %= mod;
        }
    }

    writeln(ans);
}































void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0