結果

問題 No.30 たこやき工場
ユーザー mugen_1337mugen_1337
提出日時 2021-04-09 12:06:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 5,070 bytes
コンパイル時間 2,536 ms
コンパイル使用メモリ 218,860 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 05:24:06
合計ジャッジ時間 3,406 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 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 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.cpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    size_t size(){
        return g.size();
    }
    void resize(int k){
        g.resize(k);
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};

// scc.belong[i]  : strongly connected components i belongs 
// scc.group[i]   : vertice i-th strongly connected component has
// scc.compressed : compressed Graph, DAG
// Longest Path verified : https://atcoder.jp/contests/abc135/submissions/19684261
template<typename T=int>
struct StronglyConnectedComponents{
    private:
    Graph<T> g,rg;
    vector<int> check;
    void dfs(int cur,vector<int> &ord){
        for(auto &to:g[cur])if(!check[to]){
            check[to]=true;
            dfs(to,ord);
        }
        ord.push_back(cur);
    }
    void rdfs(int cur,int p){
        for(auto &to:rg[cur])if(belong[to]==-1){
            belong[to]=p;
            rdfs(to,p);
        }
    }

    void build(){
        vector<int> ord;
        for(int i=0;i<(int)g.size();i++)if(!check[i]){
            check[i]=true;
            dfs(i,ord);
        }
        int ptr=0;;
        for(int i=(int)ord.size()-1;i>=0;i--)if(belong[ord[i]]==-1){
            belong[ord[i]]=ptr;
            rdfs(ord[i],ptr);ptr++;
        }
        compressed.resize(ptr);
        group.resize(ptr);
        for(int i=0;i<(int)g.size();i++){
            int u=belong[i];
            group[u].push_back(i);
            for(auto &e:g[i]){
                int v=belong[e];
                if(u!=v) compressed.add_directed_edge(u,v,e.w);
            }
        }
        return ;
    }

    public:
    vector<int> belong;
    vector<vector<int>> group;
    Graph<T> compressed;
    
    StronglyConnectedComponents(Graph<T> &g):g(g),rg(g.size()),check(g.size()),belong(g.size(),-1){
        for(int i=0;i<(int)g.size();i++)for(auto &e:g[i]) rg.add_directed_edge(e.to,e.from,e.w);
        build();
    }

    // topological sort
    vector<int> get_DAG_order(){
        vector<int> ret;
        for(int i=0;i<(int)group.size();i++)for(auto &j:group[i]) ret.push_back(j);
        return ret;
    }

    // g is not DAG or contain self-loop, return inf
    T LongestPath(){
        for(int i=0;i<(int)g.size();i++){
            for(auto &e:g[i]){
                if(belong[i]==belong[e]) return -1;                
            }
        }
        vector<int> ord=get_DAG_order();
        vector<T> dp(g.size(),0);
        for(auto i:ord)for(auto &e:g[i]) dp[e]=max(dp[e],dp[i]+e.w);
        return (*max_element(begin(dp),end(dp)));
    }
};

signed main(){
    int n,m;cin>>n>>m;
    Graph<ll> g(n),rg(n);
    rep(i,m){
        int u,v;ll c;
        cin>>u>>c>>v;u--,v--;
        g.add_directed_edge(u,v,c);
        rg.add_directed_edge(v,u,c);
    }

    StronglyConnectedComponents<ll> scc(g);
    auto ord=scc.get_DAG_order();
    reverse(ALL(ord));

    vector<ll> dp(n,0);
    dp[n-1]=1;
    for(auto &i:ord){
        if(rg[i].empty()) continue;
        for(auto &e:rg[i]){
            dp[e]+=dp[i]*e.w;
        }
        dp[i]=0;
    }
    rep(i,n-1) cout<<dp[i]<<endl;
    return 0;
}
0