結果

問題 No.2532 Want Play More
ユーザー hitonanode
提出日時 2023-11-03 22:33:04
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 806 bytes
コンパイル時間 1,135 ms
コンパイル使用メモリ 92,044 KB
実行使用メモリ 20,988 KB
最終ジャッジ日時 2024-09-25 20:57:29
合計ジャッジ時間 3,248 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    cin >> N;
    vector<vector<int>> to(N);
    for (int e = 0; e < N - 1; ++e) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        to.at(a).push_back(b);
        to.at(b).push_back(a);
    }

    auto rec = [&](auto &&self, int now, int prv, bool f) -> int {
        int cur = f ? 0 : 1 << 20;

        int nch = 0;
        for (int nxt : to.at(now)) {
            if (nxt == prv) continue;
            ++nch;
            if (int tmp = self(self, nxt, now, !f) + 1; (f and cur < tmp) or (!f and cur > tmp)) cur = tmp;
        }

        return nch ? cur : 0;
    };

    cout << rec(rec, 0, -1, true) << '\n';
    cout << rec(rec, 0, -1, false) << '\n';
}
0