結果

問題 No.30 たこやき工場
ユーザー T1610T1610
提出日時 2019-04-01 01:16:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,000 bytes
コンパイル時間 1,815 ms
コンパイル使用メモリ 179,548 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 05:21:01
合計ジャッジ時間 2,633 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,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

// #define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << endl
#define LINE cout << "line : " << __LINE__ << endl
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) ;
#define LINE     ;
#define dumpV(v);
#define STOP     ;
#endif
#define mp make_pair

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fi<<", "<<a.se<<')';
    return out;
  }
}


struct SCC{//O(V+E)
    static const int MAX_V = 100+10;
    int V;
    vector<int> G[MAX_V], rG[MAX_V];
    vector<int> vs;
    bool used[MAX_V];
    int cmp[MAX_V];
    vector<vector<int> > res;


    SCC(int v) : V(v){assert(v < MAX_V);};
    void add_edge(int from, int to){
        G[from].push_back(to);
        rG[to].push_back(from);
    }

    void dfs(int v){
        used[v] = true;
        for(int i = 0; i < G[v].size(); i++){
            if(!used[G[v][i]]) dfs(G[v][i]);
        }
        vs.push_back(v);
    }
    void rdfs(int v, int k){
        used[v] = true;
        cmp[v] = k;
        for(int i = 0; i < rG[v].size(); i++){
            if(!used[rG[v][i]]) rdfs(rG[v][i], k);
        }
    }

    int scc(){
        memset(used, 0, sizeof(used));
        vs.clear();
        for(int v = 0; v < V; v++){
            if(!used[v]) dfs(v);
        }
        memset(used, 0, sizeof(used));
        int k = 0;
        for(int i = vs.size() - 1; i >= 0; i--){
            if(!used[vs[i]]) rdfs(vs[i], k++);
        }

        res.clear();
        res.resize(k);
        for(int i = 0; i < V; i++){
            res[cmp[i]].push_back(i);
        }
        return k;
    }
};

struct Edge {
    int to, cost;
};
vector<Edge> es[SCC::MAX_V];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    SCC scc(n);
    rep(i, m) {
        int p, q, r;
        cin >> p >> q >> r;
        --p;
        --r;
        es[r].pb({p, q});
        scc.add_edge(r, p);
    }
    scc.scc();
    vector<ll> cnt(n);
    cnt.back() = 1;
    auto& vv = scc.res;
    for(auto&& v : vv) {
        dumpV(v);
        dumpV(cnt);
        for(auto& i : v) {
            for(auto& e : es[i]) {
                int j = e.to;
                cnt[j] += cnt[i] * e.cost;
            }
            if(es[i].size() > 0) cnt[i] = 0;
        }
    } 
    rep(i, n-1) cout << cnt[i] << '\n';

    return 0;
}
0