結果

問題 No.762 PDCAパス
ユーザー lunnear
提出日時 2018-12-11 15:08:00
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,359 bytes
コンパイル時間 730 ms
コンパイル使用メモリ 72,368 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-09-21 22:09:35
合計ジャッジ時間 3,705 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <list>
#include <map>

#define MOD 1000000007

unsigned long long count;
char *s;
std::list<int> *to;
std::map<char,int> pdca;

void Serch(int index)
{
    for(auto i = to[index].begin(); i != to[index].end(); i++)
    {
        if(s[index] == 'C')
        {
            count += to[index].size();
            break;
        }
        
        if(pdca[s[index]] < pdca[s[(*i)]])
        {
            Serch((*i));
        }
    }
}

int main()
{
    pdca['P'] = 0;
    pdca['D'] = 1;
    pdca['C'] = 2;
    pdca['A'] = 3;
    
    
    int n, m;
    std::cin >> n >> m;
    
    to = new std::list<int>[n];
    s = new char[n + 1];
    std::cin >> s;
    
    for(int i = 0; i < m; i++)
    {
        int n1, n2;
        std::cin >> n1 >> n2;
        n1--;
        n2--;
        
        int sub = pdca[s[n1]] - pdca[s[n2]];
        if(abs(sub) == 1)
        {
            if(sub < 0)
                to[n1].push_back(n2);
            else
                to[n2].push_back(n1);
        }
    }
    
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        if(s[i] != 'P' || to[i].empty())
            continue;
        
        count = 0;
        Serch(i);
        count %= MOD;
        ans += count;
        ans %= MOD;
    }
    
    std::cout << ans << std::endl;
    
    delete[] s;
    delete[] to;
    
    return 0;
}
0