procon

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub mugen1337/procon

:heavy_check_mark: test/yosupo_scc2.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/scc"

#include "../template.hpp"

#include "../Graph2/StronglyConnectedComponents.hpp"

signed main(){
    int n,m;cin>>n>>m;
    Graph g(n);
    g.read(m,0,false,true);

    StronglyConnectedComponents scc(g);

    cout<<scc.group.size()<<"\n";
    rep(i,(int)scc.group.size()){
        cout<<scc.group[i].size();
        for(auto x:scc.group[i]) cout<<" "<<x;
        cout<<"\n";
    }
    return 0;
}
#line 1 "test/yosupo_scc2.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/scc"

#line 1 "template.hpp"
#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 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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;
}

#line 4 "test/yosupo_scc2.test.cpp"

#line 1 "Graph2/GraphTemplate.hpp"



// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.hpp
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){}

    int size(){
        return (int)g.size();
    }
    void resize(int k){
        g.resize(k);
        V=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);
        }
    }
};


#line 2 "Graph2/StronglyConnectedComponents.hpp"
// 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)));
    }
};
#line 6 "test/yosupo_scc2.test.cpp"

signed main(){
    int n,m;cin>>n>>m;
    Graph g(n);
    g.read(m,0,false,true);

    StronglyConnectedComponents scc(g);

    cout<<scc.group.size()<<"\n";
    rep(i,(int)scc.group.size()){
        cout<<scc.group[i].size();
        for(auto x:scc.group[i]) cout<<" "<<x;
        cout<<"\n";
    }
    return 0;
}
Back to top page