結果

問題 No.3206 う し た ウ ニ 木 あ く ん 笑
ユーザー umimel
提出日時 2025-07-18 21:39:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 197 ms / 3,000 ms
コード長 1,799 bytes
コンパイル時間 1,819 ms
コンパイル使用メモリ 176,176 KB
実行使用メモリ 71,976 KB
最終ジャッジ日時 2025-07-18 21:40:26
合計ジャッジ時間 5,140 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


void solve(){
    int n; cin >> n;
    vector<vector<int>> T(n);
    for(int i=0; i<n-1; i++){
        int u, v; cin >> u >> v;
        u--; v--;
        T[u].push_back(v);
        T[v].push_back(u);
    }

    vector<int> dp1(n, 1);
    function<void(int, int)> dfs1 = [&](int v, int p) {
        for(int to : T[v]) if(to != p){
            dfs1(to, v);
            dp1[v] = max(dp1[v], dp1[to]+1);
        }
    }; dfs1(0, -1);

    int ans = 0;
    vector<int> dp2(n, 0);
    function<void(int, int)> dfs2 = [&](int v, int p) {
        {
            vector<int> x;
            if(v != 0) x.pb(dp2[v]);
            for(int to : T[v]) if(to != p) x.pb(dp1[to]);
            sort(all(x), greater<int>());
            for(int i=0; i<x.size(); i++){
                ans = max(ans, x[i]*(i+1) + 1);
            }
        }

        multiset<int> mst;
        mst.insert(dp2[v]);
        for(int to : T[v]) if(to != p) mst.insert(dp1[to]);
        for(int to : T[v]) if(to != p){
            mst.erase(mst.find(dp1[to]));
            dp2[to] = 1 + (mst.empty() ? 0 : *mst.rbegin());
            mst.insert(dp1[to]);
        }
        for(int to : T[v]) if(to != p) dfs2(to, v);
    }; dfs2(0, -1);

    cout << ans << '\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0