procon

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

View the Project on GitHub mugen1337/procon

:heavy_check_mark: Low Link
(Graph2/LowLink.hpp)

概要

https://kagamiz.hatenablog.com/entry/2013/10/05/005213
dfs木作る.
ord[u] : dfsの訪問順
low[u] : uから後退辺を1度だけ使って一番できる根側の頂点

u->vの辺がある時,ord[u]<low[v]なら橋

関節点の判定も可能.

仕様

計算量

頂点数をV, 変数をEとする
dfsしかしていないため
O(V+E)

Depends on

Required by

Verified with

Code

#include "./GraphTemplate.hpp"

template<typename T>
struct LowLink{
    Graph<T> &g;
    vector<int> ord,low;
    vector<int> art;// articulation (true/false)
    vector<Edge<T>> bridge;

    LowLink(Graph<T> &g):g(g){
        ord.assign(g.V,-1);
        low.assign(g.V,-1);
        art.resize(g.V);

        int idx=0;
        function<void(int,int)> dfs=[&](int pre,int cur){
            ord[cur]=idx++;
            low[cur]=ord[cur];

            int pre_c=0,ch=0;
            bool art_f=false;

            for(auto &e:g[cur]){
                if(e==pre and pre_c==0){
                    pre_c++;
                    continue;
                }
                if(ord[e]<0){
                    ch++;
                    dfs(cur,e);
                    low[cur]=min(low[cur],low[e]);
                    art_f|=(pre>=0 and low[e]>=ord[cur]);
                    if(ord[cur]<low[e]) bridge.push_back(e);
                }
                else{
                    low[cur]=min(low[cur],ord[e]);
                }
            }
            if(pre==-1) art_f|=(ch>1);
            art[cur]=art_f;
            return ;
        };
        for(int i=0;i<g.V;i++)if(ord[i]<0) dfs(-1,i);
    }
};
#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/LowLink.hpp"

template<typename T>
struct LowLink{
    Graph<T> &g;
    vector<int> ord,low;
    vector<int> art;// articulation (true/false)
    vector<Edge<T>> bridge;

    LowLink(Graph<T> &g):g(g){
        ord.assign(g.V,-1);
        low.assign(g.V,-1);
        art.resize(g.V);

        int idx=0;
        function<void(int,int)> dfs=[&](int pre,int cur){
            ord[cur]=idx++;
            low[cur]=ord[cur];

            int pre_c=0,ch=0;
            bool art_f=false;

            for(auto &e:g[cur]){
                if(e==pre and pre_c==0){
                    pre_c++;
                    continue;
                }
                if(ord[e]<0){
                    ch++;
                    dfs(cur,e);
                    low[cur]=min(low[cur],low[e]);
                    art_f|=(pre>=0 and low[e]>=ord[cur]);
                    if(ord[cur]<low[e]) bridge.push_back(e);
                }
                else{
                    low[cur]=min(low[cur],ord[e]);
                }
            }
            if(pre==-1) art_f|=(ch>1);
            art[cur]=art_f;
            return ;
        };
        for(int i=0;i<g.V;i++)if(ord[i]<0) dfs(-1,i);
    }
};
Back to top page