Submit Info #23285

Problem Lang User Status Time Memory
Dominator Tree cpp hotman AC 76 ms 31.23 MiB

ケース詳細
Name Status Time Memory
example_00 AC 0 ms 0.64 MiB
example_01 AC 1 ms 0.61 MiB
random_00 AC 29 ms 20.73 MiB
random_01 AC 76 ms 31.23 MiB
random_02 AC 19 ms 10.42 MiB
random_03 AC 38 ms 27.20 MiB
random_04 AC 3 ms 2.91 MiB

#line 1 "code.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/shortest_path" #line 2 "cpplib/graph_tree/dominator_tree.hpp" #include<vector> #include<stack> #line 3 "cpplib/dsu/uf_min.hpp" #include<numeric> #include<limits> /** * @brief 根とのPathの中での最小値を返すUnionFind */ template<typename T> struct uf_min{ constexpr static int inf=std::numeric_limits<T>::max(); std::vector<int>par,mnid; std::vector<T>mn; uf_min(int v){ par.resize(v); mn.resize(v,inf); mnid.resize(v); std::iota(par.begin(),par.end(),0); std::iota(mnid.begin(),mnid.end(),0); } int find(int v){ if(par[v]==v)return v; int r=find(par[v]); if(mn[v]>mn[par[v]]){ mnid[v]=mnid[par[v]]; mn[v]=mn[par[v]]; } par[v]=r; return r; } void set(int v,T x){ mn[v]=x; } T eval(int v){ find(v); return mnid[v]; } //xをyの親にする void link(int x,int y){ par[y]=x; } }; #line 5 "cpplib/graph_tree/dominator_tree.hpp" /** * @brief 支配木 */ // rootからvに向かう際に絶対通らないといけない頂点を // vの祖先とするように木を構築する std::vector<int> dominator_tree(std::vector<std::vector<int>>g,int s){ int n=g.size(); std::vector<std::vector<int>> rev_g(n); for(int i=0;i<n;++i){ for(auto e:g[i]){ rev_g[e].push_back(i); } } std::stack<int>stk; std::vector<bool>used(n,0); std::vector<int>id(n+1,n); std::vector<int>id2(n+1,n); std::vector<int>sdom(n,n); std::vector<int>idom(n,n); std::vector<std::vector<int>>ch(n); sdom[s]=s; int idx=0; stk.emplace(s); while(!stk.empty()){ auto v=stk.top(); stk.pop(); id2[v]=idx; id[idx++]=v; for(auto e:g[v]){ if(!used[e]){ ch[v].push_back(e); stk.emplace(e); used[e]=1; } } } for(int i=0;i<n;++i){ int v=id[i]; if(v==n)continue; if(v==s)continue; for(auto e:rev_g[v]){ if(id2[e]<i){ if(id2[sdom[v]]>id2[e]){ sdom[v]=e; } } } } uf_min<int> uf(n); std::vector<std::vector<int>>sdom_list(n+1); std::vector<int>u(n,-1); for(int i=n-1;i>=0;--i){ int v=id[i]; if(v==n)continue; for(auto e:sdom_list[v]){ u[e]=uf.eval(e); } if(v==s)continue; for(auto e:rev_g[v]){ if(id2[e]<i)continue; auto d=uf.eval(e); if(id2[sdom[v]]>id2[sdom[d]])sdom[v]=sdom[d]; } sdom_list[sdom[v]].push_back(v); uf.set(v,id2[sdom[v]]); for(auto e:ch[v]){ uf.link(v,e); } } for(int i=0;i<n;++i){ auto v=id[i]; if(v==n)continue; if(v==s){ idom[v]=v; continue; } if(sdom[v]==sdom[u[v]])idom[v]=sdom[v]; else idom[v]=idom[u[v]]; } for(int i=0;i<n;++i)if(idom[i]==n)idom[i]=-1; return idom; } #line 3 "cpplib/graph_tree/graph_template.hpp" #include<tuple> #include<iostream> /** * @brief グラフテンプレート */ using graph=std::vector<std::vector<int>>; template<typename T> using graph_w=std::vector<std::vector<std::pair<int,T>>>; graph load_graph(int n,int m){ graph g(n); for(int i=0;i<m;++i){ int s,t; std::cin>>s>>t; --s;--t; g[s].push_back(t); g[t].push_back(s); } return g; } graph load_digraph(int n,int m){ graph g(n); for(int i=0;i<m;++i){ int s,t; std::cin>>s>>t; --s;--t; g[s].push_back(t); } return g; } graph load_graph0(int n,int m){ graph g(n); for(int i=0;i<m;++i){ int s,t; std::cin>>s>>t; g[s].push_back(t); g[t].push_back(s); } return g; } graph load_digraph0(int n,int m){ graph g(n); for(int i=0;i<m;++i){ int s,t; std::cin>>s>>t; g[s].push_back(t); } return g; } graph load_tree(int n){ graph g(n); for(int i=0;i<n-1;++i){ int s,t; std::cin>>s>>t; --s;--t; g[s].push_back(t); g[t].push_back(s); } return g; } graph load_tree0(int n){ graph g(n); for(int i=0;i<n-1;++i){ int s,t; std::cin>>s>>t; g[s].push_back(t); g[t].push_back(s); } return g; } graph load_treep(int n){ graph g(n); for(int i=0;i<n-1;++i){ int t; std::cin>>t; g[i+1].push_back(t); g[t].push_back(i+1); } return g; } template<typename T> graph_w<T> load_graph_weight(int n,int m){ graph_w<T> g(n); for(int i=0;i<m;++i){ int s,t; T u; std::cin>>s>>t>>u; --s;--t; g[s].emplace_back(t,u); g[t].emplace_back(s,u); } return g; } template<typename T> graph_w<T> load_digraph_weight(int n,int m){ graph_w<T> g(n); for(int i=0;i<m;++i){ int s,t; T u; std::cin>>s>>t>>u; --s;--t; g[s].emplace_back(t,u); } return g; } template<typename T> graph_w<T> load_graph0_weight(int n,int m){ graph_w<T> g(n); for(int i=0;i<m;++i){ int s,t; T u; std::cin>>s>>t>>u; g[s].emplace_back(t,u); g[t].emplace_back(s,u); } return g; } template<typename T> graph_w<T> load_digraph0_weight(int n,int m){ graph_w<T> g(n); for(int i=0;i<m;++i){ int s,t; T u; std::cin>>s>>t>>u; g[s].emplace_back(t,u); } return g; } template<typename T> graph_w<T> load_tree_weight(int n){ graph_w<T> g(n); for(int i=0;i<n-1;++i){ int s,t; T u; std::cin>>s>>t>>u; --s;--t; g[s].emplace_back(t,u); g[t].emplace_back(s,u); } return g; } template<typename T> graph_w<T> load_tree0_weight(int n){ graph_w<T> g(n); for(int i=0;i<n-1;++i){ int s,t; T u; std::cin>>s>>t>>u; g[s].emplace_back(t,u); g[t].emplace_back(s,u); } return g; } template<typename T> graph_w<T> load_treep_weight(int n){ graph_w<T> g(n); for(int i=0;i<n-1;++i){ int t; T u; std::cin>>t>>u; g[i+1].emplace_back(t,u); g[t].emplace_back(i+1,u); } return g; } #line 2 "cpplib/util/template.hpp" #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") #include<bits/stdc++.h> using namespace std; struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}__INIT__; typedef long long lint; #define INF (1LL<<60) #define IINF (1<<30) #define EPS (1e-10) #define endl ('\n') typedef vector<lint> vec; typedef vector<vector<lint>> mat; typedef vector<vector<vector<lint>>> mat3; typedef vector<string> svec; typedef vector<vector<string>> smat; template<typename T>inline void numout(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i<INF/2?i:"INF";f=1;}cout<<endl;} template<typename T>inline void numout2(T t){for(auto i:t)numout(i);} template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;} template<typename T>inline void output2(T t){for(auto i:t)output(i);} template<typename T>inline void _output(T t){bool f=0;for(lint i=0;i<t.size();i++){cout<<f?"":" "<<t[i];f=1;}cout<<endl;} template<typename T>inline void _output2(T t){for(lint i=0;i<t.size();i++)output(t[i]);} #define rep(i,...) for(auto i:range(__VA_ARGS__)) #define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__))) #define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i) #define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i) #define irep(i) for(lint i=0;;++i) inline vector<long long> range(long long n){if(n<=0)return vector<long long>();vector<long long>v(n);iota(v.begin(),v.end(),0LL);return v;} inline vector<long long> range(long long a,long long b){if(b<=a)return vector<long long>();vector<long long>v(b-a);iota(v.begin(),v.end(),a);return v;} inline vector<long long> range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector<long long>();vector<long long>v((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;} template<typename T>inline T reversed(T v){reverse(v.begin(),v.end());return v;} #define all(n) begin(n),end(n) template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;} template<typename T,typename E>bool chmax(T& s,const E& t){bool res=s<t;s=max<T>(s,t);return res;} const vector<lint> dx={1,0,-1,0,1,1,-1,-1}; const vector<lint> dy={0,1,0,-1,1,-1,1,-1}; #define SUM(v) accumulate(all(v),0LL) template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));} #line 5 "code.cpp" int main(){ lint n,m,s; cin>>n>>m>>s; auto g=load_digraph0(n,m); output(dominator_tree(g,s)); }