結果

問題 No.762 PDCAパス
ユーザー tetsuzuki1115tetsuzuki1115
提出日時 2019-08-26 02:27:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,591 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 102,300 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-25 00:08:03
合計ジャッジ時間 3,792 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 44 ms
5,376 KB
testcase_23 AC 45 ms
5,376 KB
testcase_24 AC 70 ms
8,832 KB
testcase_25 AC 71 ms
8,960 KB
testcase_26 AC 46 ms
5,376 KB
testcase_27 AC 46 ms
5,376 KB
testcase_28 AC 79 ms
7,424 KB
testcase_29 AC 73 ms
7,552 KB
testcase_30 AC 73 ms
6,528 KB
testcase_31 AC 72 ms
6,656 KB
testcase_32 AC 72 ms
6,656 KB
testcase_33 AC 69 ms
5,888 KB
testcase_34 AC 69 ms
5,888 KB
testcase_35 AC 68 ms
5,632 KB
testcase_36 AC 60 ms
5,376 KB
testcase_37 AC 64 ms
5,376 KB
testcase_38 AC 62 ms
5,376 KB
testcase_39 AC 62 ms
5,376 KB
testcase_40 AC 60 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <limits.h>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

long long MOD = 1000000007;

vector< vector<int> > P;
string S;
vector< long long > memo;


bool is_PDCA( char a, char b ) {
    bool ret = false;
    string pdca = "PDCA";
    for ( int i = 0; i < 3; i++ ) {
        if ( pdca[i] == a && pdca[i+1] == b ) { ret = true; }
    }
    return ret;
}

long long dfs( int v ) {

    if ( memo[v] != -1 ) { return memo[v]; }
    if ( S[v] == 'A' ) { return 1; }

    long long ret = 0;
    for ( int i = 0; i < P[v].size(); i++ ) {
        ret += dfs( P[v][i] );
        ret %= MOD;
    }
    memo[v] = ret;
    return ret;
}

int main() {

    int N,M;
    cin >> N >> M >> S;
    
    P.resize(N);
    memo.resize(N);
    for ( int i = 0; i < N; i++ ) {
        memo[i] = -1;
    }

    vector<int> start;
    for ( int i = 0; i < S.length(); i++ ) {
        if ( S[i] == 'P' ) { start.push_back(i); }
    }

    for ( int i = 0; i < M; i++ ) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        if ( is_PDCA( S[a], S[b] ) ) {
            P[a].push_back( b );
        }
        if ( is_PDCA( S[b], S[a] ) ) {
            P[b].push_back( a );
        }        
    }

    long long ans = 0;
    for ( int i = 0; i < start.size(); i++ ) {
        ans += dfs( start[i] );
        ans %= MOD;
    }

    cout << ans << endl;


    return 0;
}
0