結果

問題 No.3206 う し た ウ ニ 木 あ く ん 笑
ユーザー zawakasu
提出日時 2025-07-18 21:58:24
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 273 ms / 3,000 ms
コード長 3,059 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 134,936 KB
実行使用メモリ 68,096 KB
最終ジャッジ日時 2025-07-18 21:58:46
合計ジャッジ時間 5,422 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
namespace ranges = std::ranges;
namespace views = std::views;
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
int N;
std::vector<int> g[200020];
int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cin >> N;
    for (int i = 0 ; i < N - 1 ; i++) {
        int u, v;
        std::cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    std::vector<int> dp(N);
    auto rec = [&](auto rec, int v, int p) -> int {
        std::vector<int> can;
        can.reserve(g[v].size());
        for (int x : g[v]) if (x != p) dp[v] = std::max(dp[v], rec(rec, x, v));
        return ++dp[v];
    };
    rec(rec, 0, -1);
    // for (int v : dp) std::cout << v << ' ';
    // std::cout << std::endl;
    int ans = 0;
    auto rec2 = [&](auto rec2, int v, int p, int pv) -> void {
        std::vector<int> can;
        std::vector<std::pair<int, int>> vs;
        for (int x : g[v]) {
            if (x == p) {
                can.push_back(pv);
                vs.push_back({pv, p});
            }
            else {
                can.push_back(dp[x]);
                vs.push_back({dp[x], x});
            }
        }
        if (p == -1) vs.push_back({pv, p});
        // std::cout << v + 1 << " ";
        // for (auto [val, i] : vs) std::cout << '(' << val << ',' << i + 1 << ')';
        // std::cout << std::endl;
        ranges::sort(can, std::greater<int>{});
        for (int i = 0 ; i < std::ssize(can) ; i++) {
            const int val = can[i] * (i + 1) + 1;
            ans = std::max(ans, val);
        }
        int max = -1, max2 = -1;
        for (int i = 0 ; i < std::ssize(vs) ; i++) {
            if (max == -1 or vs[max].first < vs[i].first) {
                max2 = max;
                max = i;
            }
            else if (max2 == -1 or vs[max2].first < vs[i].first) {
                max2 = i;
            }
        }
        // std::cout << max << ' ' << max2 << std::endl;
        // std::cout << v + 1 << "!" << std::endl;
        // std::cout << max << ' ' << max2 << ' ' << vs[max].second << ' ' << vs[max2].second << std::endl;
        for (int i = 0 ; i < std::ssize(g[v]) ; i++) if (g[v][i] != p) {
            const int propa = vs[i == max ? max2 : max].first;
            rec2(rec2, g[v][i], v, propa + 1);
        }
    };
    rec2(rec2, 0, -1, 0);
    std::cout << ans << '\n';
}
0