結果

問題 No.1605 Matrix Shape
コンテスト
ユーザー eQe
提出日時 2025-10-22 06:51:32
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 8,095 bytes
コンパイル時間 4,943 ms
コンパイル使用メモリ 333,956 KB
実行使用メモリ 48,432 KB
最終ジャッジ日時 2025-10-22 06:51:44
合計ジャッジ時間 10,810 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#if __has_include(<atcoder/all>)
#endif
using namespace std;
#define eb emplace_back
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define RDVV(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);vin(__VA_ARGS__)
#define VV(n,...) RDVV(ll,n,__VA_ARGS__)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
#define ef(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):ranges::reverse_view(a))
#define binary_operator(op,type) auto operator op(const type&rhs)const{auto copy=*this;return copy op##=rhs;}
#define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);}
#define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{
namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
constexpr auto refs(auto&...a){return array{ref(a)...};}
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;}
template<class T>constexpr ll maxsize10(){return size10(numeric_limits<T>::max());}
bool amin(auto&a,const auto&b){return b<a?a=b,1:0;}
template<class A,class B=A>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  auto operator<=>(const pair&)const=default;
};
template<bool is_negative=false>struct infinity{
  template<integral T>static constexpr T ones(size_t n){return n?ones<T>(n-1)*10+1:0;}
  template<integral T>constexpr operator T()const{static constexpr T v=ones<T>(maxsize10<T>())*(1-is_negative*2);return v;}
  template<class T>constexpr bool operator==(const T&x)const{return static_cast<T>(*this)==x;}
};
constexpr infinity oo;
template<class F=less<>>auto&sort(auto&a,F f={}){ranges::sort(a,f);return a;}
auto&unique(auto&a){sort(a).erase(ranges::unique(a).begin(),a.end());return a;}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace my{
template<class T>struct unordered_set:std::unordered_set<T>{
  unordered_set()=default;
};
}
namespace my{
template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>;
template<class V>constexpr int depth=0;
template<class T>struct core_t_helper{using type=T;};
template<class T>using core_t=core_t_helper<T>::type;
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
  static constexpr int D=depth<V>+1;
  using C=core_t<V>;
  using vector<V>::vector;
  void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else{ }}
  static auto make(ll n,const auto&...a){
    if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
    else { }
  }
  auto&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
  binary_operator(^,vec)
  ll size()const{return vector<V>::size();}
  auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;}
  auto pop_back(){auto r=this->back();vector<V>::pop_back();return r;}
  auto lower_bound(const V&x)const{return std::lower_bound(this->begin(),this->end(),x);}
  ll arg_lower_bound(const V&x)const{return lower_bound(x)-this->begin();}
  auto fold(const auto&f)const{
    pair<C,bool>r{};
    fe(*this,e){
      if constexpr(!vectorial<V>){
        if(r.b)f(r.a,e);
        else r={e,1};
      }else { }

    }
    return r;
  }
  auto max()const{return fold([](auto&a,auto b){if(a<b)a=b;}).a;}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
auto zip(auto&...a){auto v=(a^...);unique(v);([&](auto&u){fe(u,e)e=v.arg_lower_bound(e);}(a),...);return v;}
}
namespace my{
template<class T>struct queue:std::queue<T>{
  queue(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);}
  ll size()const{return std::queue<T>::size();}
  T pop(){T r=this->front();std::queue<T>::pop();return r;}
};
template<class T,class F>struct priority_queue:std::priority_queue<T,vector<T>,F>{
  priority_queue(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);}
  ll size()const{return std::priority_queue<T,vector<T>,F>::size();}
  T pop(){T r=this->top();std::priority_queue<T,vector<T>,F>::pop();return r;}
};
template<class T>using min_heap=priority_queue<T,greater<T>>;
}
namespace my{
template<class WT>struct Edge{
  int from_,to_;
  WT wt_;
  int id_;
  Edge()=default;
  Edge(int from,int to,WT wt=1,int id=-1):from_(from),to_(to),wt_(wt),id_(id){}
  auto operator<=>(const Edge&e)const{return wt_<=>e.wt_;}
};
template<class WT>class Graph{
public:
  vec<vec<Edge<WT>>>edges_;
  Graph()=default;
  Graph(ll n):edges_(n){}
  decltype(auto)operator[](ll i)const{return edges_[i];}
  ll size()const{return edges_.size();}
  void add_edge(ll a,auto&&...b){edges_[a].eb(a,std::forward<decltype(b)>(b)...);}
  auto&add_edges(const vec<ll>&a,const vec<ll>&b){fo(i,a.size())edges_[a[i]].eb(a[i],b[i],1,i);return*this;}
  auto in_deg_enumerate()const{
    vec<ll>res(size());
    fo(u,size())fe(edges_[u],e)++res[e.to_];
    return res;
  }
  auto topological_order()const{
    auto deg=in_deg_enumerate();

    queue<ll>q;
    fo(i,size())if(deg[i]==0)q.emplace(i);

    vec<ll>r;
    while(q.size()){
      ll u=q.pop();
      r.eb(u);
      fe(edges_[u],e)if(--deg[e.to_]==0)q.emplace(e.to_);
    }
    return r;
  }
  Graph rev_graph()const{
    Graph res(size());
    fo(u,size())fe(edges_[u],e)res.add_edge(e.to_,e.from_,e.wt_,e.id_);
    return res;
  }
  auto dfs_post_order_groups()const{
    vec<bool>used(size());
    vec<vec<ll>>res;
    fo(i,size())if(!used[i]){
      res.eb();
      vec<ll>st{i};
      while(st.size()){
        ll u=st.pop_back();
        if(u>=0){
          if(used[u])continue;
          used[u]=1;
          st.eb(~u);

          ef(edges_[u],e)if(!used[e.to_])st.eb(e.to_);
        }else{
          res.back().eb(~u);
        }
      }
    }
    return res;
  }
  auto dijkstra_dist_enumerate(int s)const{
    vec<WT>dist(size(),oo);
    dist[s]=WT{};

    vec<bool>used(size());
    min_heap<pair<WT,Edge<WT>>>q{{WT{},{-1,s,WT{},-1}}};
    while(q.size()){
      auto[du,pu]=q.pop();
      auto u=pu.to_;
      if(dist[u]<du)continue;
      if(used[u])continue;
      used[u]=1;

      fe(edges_[u],e){
        if(amin(dist[e.to_],dist[u]+e.wt_)){
          assert(!used[e.to_]);
          q.emplace(dist[e.to_],e);
        }
      }
    }
    return dist;
  }
};
}
namespace my{
template<class WT>struct StronglyConnectedComponent:Graph<WT>{
  vec<int>group_id_;
  vec<vec<int>>group_;
  StronglyConnectedComponent(const Graph<WT>&g):group_id_(g.size(),-1){
    auto rev_g=g.rev_graph();

    ll n=0;
    ef(g.dfs_post_order_groups(),v)ef(v,u)if(group_id_[u]==-1)rev_dfs(u,n++,rev_g);

    this->edges_.resize(n);
    unordered_set<ll>s;
    ll N=g.size();
    fo(u,N)fe(g[u],e){
      ll x=group_id_[u],y=group_id_[e.to_];
      if(x!=y&&!s.contains(x*N+y)){
        this->add_edge(x,y,e.wt_,e.id_);
        s.emplace(x*N+y);
      }
    }

    group_.resize(n);
    fo(u,N)group_[group_id_[u]].eb(u);
    fe(group_,e)sort(e);
  }
  void rev_dfs(ll u,ll c,const Graph<WT>&rev_g){
    if(group_id_[u]!=-1)return;
    group_id_[u]=c;
    fe(rev_g[u],e)rev_dfs(e.to_,c,rev_g);
  }
};
}
namespace my{entry
void main(){
  LL(N);
  VV(N,a,b);
  ll M=zip(a,b).size();

  Graph<ll>g(M);
  g.add_edges(a,b);

  StronglyConnectedComponent<ll>scc(g);

  if(scc.size()==1){
    pp(M);
  }else{
    auto top=scc.topological_order();
    auto dist=scc.dijkstra_dist_enumerate(top[0]);
    pp(dist.max()==oo?0:1);
  }
}}
0