結果

問題 No.1976 Cut then Connect
コンテスト
ユーザー eQe
提出日時 2026-12-01 09:46:38
言語 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  
実行時間 91 ms / 2,000 ms
+ 124µs
コード長 6,344 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,084 ms
コンパイル使用メモリ 355,596 KB
実行使用メモリ 29,696 KB
最終ジャッジ日時 2026-12-01 09:46:47
合計ジャッジ時間 8,606 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/*
#include<kyopro/master>
void self::main(){
  LL(N);
  VV(N-1,a,b);dec(a,b);
  
  struct node{
    struct S{
      bool is_empty;
      ll diameter;
      ll max_dep;
    };
    static S trans_edge(S x,const edge_rev_index<ll>&e){
      x.max_dep++;
      return x;
    };
    static S op(S a,S b){
      if(a.is_empty)return b;
      if(b.is_empty)return a;
      return{
        false,
        max(a.diameter,b.diameter,a.max_dep+b.max_dep),
        max(a.max_dep,b.max_dep)
      };
    };
    static S trans_vertex(S x,ll u){
      return{
        false,
        max(x.diameter,x.max_dep),
        x.max_dep
      };
    };
    static S e(){return{true,0,0};}
  };
 
  RerootingDPTreeNodeAlias<ll,node>rt(N);
  rt.add_edges(a,b);
  rt.build();
  
  auto dp=rt.dp();
  ll ans=oo;
  fo(u,N){
    fo(i,rt[u].size()){
      ll v=rt[u][i].to;
      ll j=rt[u][i].rev_idx;
      amin(ans,max(
        dp[u][i].diameter,
        dp[v][j].diameter,
        ceil(dp[u][i].diameter,2)+ceil(dp[v][j].diameter,2)+1 // 接続による直径を最小化するには,中心同士を接続する.
      ));
    }
  }
  pp(ans);
}
*/

#include<bits/stdc++.h>
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)
namespace self{
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 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());}
auto ceil(auto x,auto y){if(y<0)x=-x,y=-y;return x<=0?x/y:(x-1)/y+1;}
bool amin(auto&a,const auto&b){return b<a?a=b,1:0;}
auto max(auto...a){return max(initializer_list<common_type_t<decltype(a)...>>{a...});}
template<bool neg=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{return ones<T>(maxsize10<T>())*(1-neg*2);}
};
constexpr infinity oo;
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>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>{
  using vector<V>::vector;
  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 WT>struct edge_rev_index{
  int from,to;
  WT wt;
  int id;
  int rev_idx;
  edge_rev_index()=default;
  edge_rev_index(int from,int to,WT wt,int id,int rev_idx):from(from),to(to),wt(wt),id(id),rev_idx(rev_idx){}
};
}
namespace self{
template<class WT,class S,S trans_edge(S,const edge_rev_index<WT>&),S op(S,S),S trans_vertex(S,ll),S e()>class RerootingDPTree{
private:
  ll n_;
  vec<vec<edge_rev_index<WT>>>edges_;
  vec<vec<S>>dp_;
public:
  RerootingDPTree(ll n):n_(n),edges_(n),dp_(n){}
  const auto&operator[](ll i)const{return edges_[i];}
  void add_edges(const vec<ll>&a,const vec<ll>&b){
    fo(i,a.size()){
      edges_[a[i]].eb(a[i],b[i],1,i,edges_[b[i]].size());
      edges_[b[i]].eb(b[i],a[i],1,i,edges_[a[i]].size()-1);
    }
  }
  ll deg(ll u)const{return edges_[u].size();}
  S init_dfs(ll u,ll p){
    S cum=e();
    dp_[u].resize(deg(u));
    fo(i,deg(u))if(ll v=edges_[u][i].to;v!=p){
      dp_[u][i]=trans_edge(init_dfs(v,u),edges_[u][i]);
      cum=op(cum,dp_[u][i]);
    }
    return trans_vertex(cum,u);
  };
  void reroot_dfs(ll u,ll p){
    vec left(deg(u),e()),right(deg(u),e());
    fo(i,left.size()-1)left[i+1]=op(left[i],dp_[u][i]);
    of(i,right.size(),1)right[i-1]=op(dp_[u][i],right[i]);

    fo(i,deg(u))if(ll v=edges_[u][i].to;v!=p){
      ll j=edges_[u][i].rev_idx;
      dp_[v][j]=trans_edge(trans_vertex(op(left[i],right[i]),u),edges_[v][j]);
      reroot_dfs(v,u);
    }
  };
  void build(){
    init_dfs(0,-1);
    reroot_dfs(0,-1);
  }
  auto dp()const{return dp_;}
};
template<class WT,class M>using RerootingDPTreeNodeAlias=RerootingDPTree<WT,typename M::S,M::trans_edge,M::op,M::trans_vertex,M::e>;
}
void self::pp(auto&&...a){const char*c="";((cout<<c<<a,c=" "),...);cout<<'\n';}
void self::main(){
  LL(N);
  VV(N-1,a,b);dec(a,b);

  struct node{
    struct S{
      bool is_empty;
      ll diameter;
      ll max_dep;
    };
    static S trans_edge(S x,const edge_rev_index<ll>&e){
      x.max_dep++;
      return x;
    };
    static S op(S a,S b){
      if(a.is_empty)return b;
      if(b.is_empty)return a;
      return{
        false,
        max(a.diameter,b.diameter,a.max_dep+b.max_dep),
        max(a.max_dep,b.max_dep)
      };
    };
    static S trans_vertex(S x,ll u){
      return{
        false,
        max(x.diameter,x.max_dep),
        x.max_dep
      };
    };
    static S e(){return{true,0,0};}
  };

  RerootingDPTreeNodeAlias<ll,node>rt(N);
  rt.add_edges(a,b);
  rt.build();

  auto dp=rt.dp();
  ll ans=oo;
  fo(u,N){
    fo(i,rt[u].size()){
      ll v=rt[u][i].to;
      ll j=rt[u][i].rev_idx;
      amin(ans,max(
        dp[u][i].diameter,
        dp[v][j].diameter,
        ceil(dp[u][i].diameter,2)+ceil(dp[v][j].diameter,2)+1
      ));
    }
  }
  pp(ans);
}
0