結果

問題 No.3206 う し た ウ ニ 木 あ く ん 笑
ユーザー igeee
提出日時 2025-07-18 22:05:06
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 307 ms / 3,000 ms
コード長 8,323 bytes
コンパイル時間 6,369 ms
コンパイル使用メモリ 334,356 KB
実行使用メモリ 104,884 KB
最終ジャッジ日時 2025-07-18 22:05:18
合計ジャッジ時間 10,913 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
ll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}
ll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
  
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;

//ref https://github.com/drken1215/algorithm/blob/master/Tree/rerooting_with_edge.cpp
//verify https://atcoder.jp/contests/abc348/submissions/52401893
/*
    通常の木 DP において、頂点 v を根とする部分根付き木に関する再帰関数 dfs(v) について、
    1. res = IDENTITY
    2. 頂点 v の各子頂点 v2 (その辺を e とする) に対して:res = MERGE(res, ADDEDGE(e, rec(v2)))
    3. return ADDNODE(v, res)
   というような更新を行うものとする。
   このような木 DP を全方位木 DP へと拡張する。
    using Graph = vector<vector<E>>;
    using GetIdFunc = function<ll(E)>;//Eから辺の行き先を取り出す
    using AddEdgeFunc = function<M(E, M)>;//情報を持ち上げるときにやりたい操作
    using MergeFunc = function<M(M, M)>;//情報のマージ
    using AddNodeFunc = function<M(ll, M)>;//頂点の情報で反映させたいもの
*/
template<class M,class E>
struct ReRooting{
  using Graph = vector<vector<E>>;
  using GetIdFunc = function<ll(E)>;//Eから辺の行き先を取り出す
  using AddEdgeFunc = function<M(E, M)>;//情報を持ち上げるときにやりたい操作
  using MergeFunc = function<M(M, M)>;//情報のマージ
  using AddNodeFunc = function<M(ll, M)>;//頂点の情報で反映させたいもの

  Graph g;
  M e;
  GetIdFunc getid;
  AddEdgeFunc addedge;
  MergeFunc merge;
  AddNodeFunc addnode;

  vector<vector<M>> dp;

  ReRooting(){}
  ReRooting(const Graph &_g,const M &_e,const GetIdFunc &_getid,const AddEdgeFunc &_addedge,const MergeFunc &_merge,const AddNodeFunc &_addnode){
    g = _g;
    e = _e;
    getid = _getid;
    addedge = _addedge;
    merge = _merge;
    addnode = _addnode;
    build();
  }

  //木DPする
  M rooting(ll v,ll par){
    M ret = e;
    dp[v].assign((ll)g[v].size(),e);
    rep(i,g[v].size()){
      ll nv = getid(g[v][i]);
      if(nv == par)continue;
      dp[v][i] = rooting(nv,v);
      ret = merge(ret,addedge(g[v][i],dp[v][i]));
    }
    return addnode(v,ret);
  }

  void rerooting(ll v,ll par,M pval){
    rep(i,g[v].size()){
      ll nv = getid(g[v][i]);
      if(nv == par){
        dp[v][i] = pval;//親から来たやつ
        continue;
      }
    }
    //左右累積計算
    vector<M> left(g[v].size() + 1,e);
    vector<M> right(g[v].size() + 1,e);
    rep(i,g[v].size()){
      ll ri = (ll)g[v].size() -1- i;
      left[i+1] = merge(left[i],addedge(g[v][i],dp[v][i]));
      right[i+1] = merge(right[i],addedge(g[v][ri],dp[v][ri]));
    }
    rep(i,g[v].size()){
      ll nv = getid(g[v][i]);
      ll ri = (ll) g[v].size()- 1 - i;
      if(nv == par)continue;
      M npval = merge(left[i],right[ri]);
      rerooting(nv,v,addnode(v,npval));
    }
  }

  void build(){
    dp.assign((ll)g.size(),vector<M>());
    ll root = 0;
    rooting(root,-1);  
    
    rerooting(root,-1,e);
  }

  M get(ll x){
    M ret = e;
    rep(i,g[x].size()){
      ret = merge(ret,addedge(g[x][i],dp[x][i]));
    }
    return addnode(x,ret);
  }
};
 
int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  LL(n);
  vvll g(n);
  rep(i,n-1){
    LL(u,v);
    u--;v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  /*
    通常の木 DP において、頂点 v を根とする部分根付き木に関する再帰関数 dfs(v) について、
    1. res = IDENTITY
    2. 頂点 v の各子頂点 v2 (その辺を e とする) に対して:res = MERGE(res, ADDEDGE(e, rec(v2)))
    3. return ADDNODE(v, res)
   というような更新を行うものとする。
   このような木 DP を全方位木 DP へと拡張する。
    using Graph = vector<vector<E>>;
    using GetIdFunc = function<ll(E)>;//Eから辺の行き先を取り出す
    using AddEdgeFunc = function<M(E, M)>;//情報を持ち上げるときにやりたい操作
    using MergeFunc = function<M(M, M)>;//情報のマージ
    using AddNodeFunc = function<M(ll, M)>;//頂点の情報で反映させたいもの
*/
  using E = ll;
  using M = ll;//最大最小
  auto getid = [&](E u)-> ll {
    return u;
  };
  auto addedge = [&](E u,M v) -> M{
    return v;
  };
  auto merge = [&](M u,M v)-> M{
    return max(u,v);
  };
  auto addnode = [&](ll a,M b) -> M {
    return  b+1;
  };
  ReRooting<M,E> rr(g,0,getid,addedge,merge,addnode);
  rr.build();

  ll ans = 1;
  //dp[i][j]にそれぞれ値が入っている
  rep(i,n){
    // chmax(ans,1 + v * sz(g[i]));
    sort(all(rr.dp[i]));
    reverse(all(rr.dp[i]));
    ll ma = INF;
    rep(j,sz(rr.dp[i])){
      chmin(ma,rr.dp[i][j]);
      chmax(ans,1+(j+1)*ma);
    }
  }
  cout << ans << endl;
}
0