結果

問題 No.3852 Partition into 2-edge paths
コンテスト
ユーザー eQe
提出日時 2026-12-04 23:25:35
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 223 ms / 2,000 ms
+ 184µs
コード長 6,330 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,493 ms
コンパイル使用メモリ 366,440 KB
実行使用メモリ 42,748 KB
最終ジャッジ日時 2026-12-04 23:25:45
合計ジャッジ時間 9,741 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/*
#include<kyopro/master>
void self::main(){
  LL(N);
  N=N*2+1;
  VV(N-1,u,v);dec(u,v);
  auto g=make_rooted_tree(N,0,u,v);
  
  use_ml998244353
  vec dp(N,2,ml{});
  // dp[u][j]:uの部分木を分ける方法で,uを端点に持つ余っている辺の個数がjである場合の数.
  ef(g.bfs_order(),u){
    dp[u][0]=1;
    dp[u][1]=0;
    
    ml A=1,B=1;
    ll Bcnt=0;
    fe(g[u],e){
      ll v=e.to;
      if(dp[v][1].val())A*=dp[v][1];
      else B*=dp[v][0],Bcnt++;
    }
    
    ll n=(odd(Bcnt)?Bcnt-1:Bcnt);
    ll c=2,k=n/c;
    dp[u][odd(Bcnt)]=A*B*fac<ml>(Bcnt)*fac_inv<ml>(c).pow(k)*fac_inv<ml>(k);
  }
  pp(dp[0][0]);
}
*/

#include<bits/stdc++.h>
#include<atcoder/modint>
using namespace std;
namespace self{void main();void sub();void pp(auto&&...a);}
int main(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);self::main();}
#define eb emplace_back
#define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define RDVV(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resize(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 of(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(1,__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 use_ml998244353 using ml=atcoder::modint998244353;
namespace self{
auto&operator<<(ostream&o,const atcoder::modint998244353&x){return o<<(int)x.val();}
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};}
template<class T>constexpr auto for_range(T s,T a,T b,T c=1){return array{a-s,b,(1-s*2)*c};}
void lin(auto&...a){(cin>>...>>a);}
void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);}
constexpr auto odd(auto x){return x&1;}
void dec(auto&...a){((--a),...);}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace self{
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<vectorial V>constexpr int depth<V> =depth<typename V::value_type>+1;
template<class T>struct core_t_helper{using type=T;};
template<vectorial V>struct core_t_helper<V>{using type=typename core_t_helper<typename V::value_type>::type;};
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;
  vec(const auto&...a)requires(sizeof...(a)>=3){assign_all(a...);}
  void assign_all(const auto&...a){if constexpr(sizeof...(a)==D){ }else*this=make(a...);}
  static auto make(ll n,const auto&...a){
    if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
    else return vec<decltype(make(a...))>(n,make(a...));
  }
  vec&operator--(){fe(*this,e)--e;return*this;}
  ll size()const{return vector<V>::size();}
  auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
}
namespace self{
template<class T>T fac(ll n){static vec<T>v{1};if(ll m=v.size();m<=n){v.resize(n+1);fo(i,m,n+1)v[i]=v[i-1]*i;}return v[n];}
template<class T>T fac_inv(ll n){static vec<T>v{1};if(ll m=v.size();m<=n){v.resize(n+1);v[n]=fac<T>(n).inv();of(i,n,m)v[i]=v[i+1]*(i+1);}return v[n];}
}
namespace self{
template<class T>struct queue:std::queue<T>{
  queue()=default;
  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;}
};
}
namespace self{
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){}
};
}
namespace self{
template<class WT>class RootedTree{
public:
  ll n_;
  vec<vec<edge<WT>>>edges_;
  vec<edge<WT>>rev_edge_;
  int root_;
  RootedTree()=default;
  RootedTree(ll n,ll r):n_(n),edges_(n),rev_edge_(n,{-1,-1,WT{},-1}),root_(r){}
  const auto&operator[](ll u)const{return edges_[u];}
  void set_edge(const edge<WT>&e){
    edges_[e.from].eb(e);
    rev_edge_[e.to]=e;
  }
  auto bfs_order(ll start=-1)const{
    if(start==-1)start=root_;
    vec<ll>ord;
    queue<ll>q{start};
    while(q.size()){
      ll u=q.pop();
      ord.eb(u);
      fe(edges_[u],e)q.emplace(e.to);
    }
    return ord;
  }
};
}
namespace self{
template<class WT>class Graph{
  ll n_;
  vec<vec<edge<WT>>>edges_;
public:
  Graph()=default;
  Graph(ll n):n_(n),edges_(n){}
  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 bfs_tree(int s)const{
    RootedTree<WT>tree(n_,s);

    vec<bool>used(n_);
    used[s]=1;
    queue<int>q;
    q.emplace(s);
    while(q.size()){
      auto u=q.pop();
      fe(edges_[u],e){
        auto v=e.to;
        if(used[v])continue;
        used[v]=1;
        tree.set_edge(e);
        q.emplace(v);
      }
    }
    return tree;
  }
};
}
namespace self{
template<class I,class WT=ll>auto make_rooted_tree(ll n,ll r,const vec<I>&a,const vec<I>&b){
  Graph<WT>g(n);
  return g.add_edges(a,b).add_edges(b,a).bfs_tree(r);
}
}
void self::pp(auto&&...a){const char*c="";((cout<<c<<a,c=" "),...);cout<<'\n';}
void self::main(){
  LL(N);
  N=N*2+1;
  VV(N-1,u,v);dec(u,v);
  auto g=make_rooted_tree(N,0,u,v);

  use_ml998244353
  vec dp(N,2,ml{});

  ef(g.bfs_order(),u){
    dp[u][0]=1;
    dp[u][1]=0;

    ml A=1,B=1;
    ll Bcnt=0;
    fe(g[u],e){
      ll v=e.to;
      if(dp[v][1].val())A*=dp[v][1];
      else B*=dp[v][0],Bcnt++;
    }

    ll n=(odd(Bcnt)?Bcnt-1:Bcnt);
    ll c=2,k=n/c;
    dp[u][odd(Bcnt)]=A*B*fac<ml>(Bcnt)*fac_inv<ml>(c).pow(k)*fac_inv<ml>(k);
  }
  pp(dp[0][0]);
}
0