結果

問題 No.763 Noelちゃんと木遊び
ユーザー nanophoto12nanophoto12
提出日時 2021-05-28 21:34:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 3,597 bytes
コンパイル時間 5,445 ms
コンパイル使用メモリ 264,308 KB
実行使用メモリ 25,852 KB
最終ジャッジ日時 2023-08-07 05:36:56
合計ジャッジ時間 7,352 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
25,852 KB
testcase_01 AC 28 ms
5,412 KB
testcase_02 AC 63 ms
8,704 KB
testcase_03 AC 43 ms
6,776 KB
testcase_04 AC 32 ms
5,720 KB
testcase_05 AC 42 ms
6,720 KB
testcase_06 AC 78 ms
9,712 KB
testcase_07 AC 77 ms
9,384 KB
testcase_08 AC 47 ms
7,008 KB
testcase_09 AC 31 ms
5,716 KB
testcase_10 AC 13 ms
4,380 KB
testcase_11 AC 87 ms
9,948 KB
testcase_12 AC 70 ms
9,136 KB
testcase_13 AC 71 ms
9,148 KB
testcase_14 AC 62 ms
8,440 KB
testcase_15 AC 43 ms
6,724 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 43 ms
6,720 KB
testcase_18 AC 81 ms
9,664 KB
testcase_19 AC 71 ms
9,220 KB
testcase_20 AC 73 ms
9,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

template<typename T>
void chmax(T& reference, T value) {
    reference = max(reference, value);
}

template<typename T>
void chmaxmap(map<T, T>& m, T key, T value) {
    if (m.count(key)) {
        chmax(m[key], value);
    }
    else {
        m[key] = value;
    }
}

template<typename T>
void chmin(T& reference, T value) {
    reference = min(reference, value);
}

struct RollingHash {
    typedef long long int_type;
    typedef pair<int_type, int_type> hash_type;

    int_type base1;
    int_type base2;
    int_type mod1;
    int_type mod2;

    vector<int_type> hash1;
    vector<int_type> hash2;
    vector<int_type> pow1;
    vector<int_type> pow2;

    RollingHash(const string& s) : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {
        init(s);
    }

    void init(const string& s) {
        int n = s.size();
        hash1.assign(n + 1, 0);
        hash2.assign(n + 1, 0);
        pow1.assign(n + 1, 1);
        pow2.assign(n + 1, 1);
        for (int i = 0; i < n; i++) {
            hash1[i + 1] = (hash1[i] + s[i]) * base1 % mod1;
            hash2[i + 1] = (hash2[i] + s[i]) * base2 % mod2;
            pow1[i + 1] = pow1[i] * base1 % mod1;
            pow2[i + 1] = pow2[i] * base2 % mod2;
        }
    }

    hash_type get(int l, int r) const {
        int_type t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
        int_type t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
        return make_pair(t1, t2);
    }

    RollingHash::hash_type concat(hash_type h1, hash_type h2, int h2_len) {
        return make_pair((h1.first * pow1[h2_len] + h2.first) % mod1,
            (h1.second * pow2[h2_len] + h2.second) % mod2);
    }

    int LCP(const RollingHash& other, int l1, int r1, int l2, int r2) {
        int len = min(r1 - l1, r2 - l2);
        int low = -1, high = len + 1;
        while (high - low > 1) {
            int mid = (low + high) / 2;
            if (get(l1, l1 + mid) == other.get(l2, l2 + mid)) low = mid;
            else high = mid;
        }
        return (low);
    }
};

#include <atcoder/all>
using namespace atcoder;

typedef modint1000000007 mint;

int main() {
    int n;
    std::cin >> n;
    vector<vector<int>> g(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<P> grid(n, P{-1,-1});
    function<P(int, int)> dfs = [&](int current, int parent) {
        if (grid[current].first != -1) {
            return grid[current];
        }
        vector<P> cs;
        for (auto x : g[current]) {
            if (x == parent) continue;
            cs.push_back(dfs(x, current));
        }
        int len = cs.size();
        if (len == 0) {
            grid[current] = { 0, 1 };
            return grid[current];
        }
        ll p1 = 0, p2 = 0, p3 = 0;
        for (auto x : cs) {
            p1 += max(x.first, x.second);
        }
        for (auto x : cs) {
            p2 += max(x.first, x.second);
        }
        for (auto x : cs) {
            p3 += x.first;
        }
        p3++;
        grid[current] = { p1, max(p2, p3) };
        return grid[current];
    };
    auto p = dfs(0, -1);
    cout << max(p.first, p.second) << endl;
    return 0;
}
0