結果

問題 No.30 たこやき工場
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-19 22:25:40
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,485 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 108,144 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-11 10:03:36
合計ジャッジ時間 1,809 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <sstream>

using namespace std;

//ABC 223 D Restricted Permutaion

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;

vector<long long> topological_sort(vector<vector<pair<long long, long long>>> &E){
    long long N = E.size(), C;
    vector<long long> deg(N), res;
    pq<long long> que;

    for (int i=0; i<N; i++){
        for (auto [j, a] : E[i]) deg[j]++;
    }


    for (int i=0; i<N; i++){
        if (deg[i] == 0) que.push(i);
    }

    while(!que.empty()){
        C = que.top();
        que.pop();
        res.push_back(C);
        for (auto [x, a] : E[C]){
            deg[x]--;
            if (deg[x] == 0) que.push(x);
        }   
    }

    return res;
}

int main(){

    long long N, M, A, B, C;
    cin >> N >> M;
    vector<vector<pair<long long, long long>>> E(N);
    vector<long long> top, deg(N), dp(N);

    for (int i=0; i<M; i++){
        cin >> A >> C >> B;
        A--; B--;
        E[A].push_back({B, C});
        deg[B]++;
    }

    top = topological_sort(E);

    dp[N-1] = 1;

    for (int i=N-1; i>=0; i--){
        for (auto [x, a] : E[top[i]]){
            dp[top[i]] += a * dp[x];
        }
    }

    for (int i=0; i<N-1; i++){
        if (deg[i] == 0) cout << dp[i] << endl;
        else cout << 0 << endl;
    }

    return 0;
}
0