結果

問題 No.1928 Make a Binary Tree
ユーザー da_ha_da_ha_
提出日時 2022-05-06 23:29:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,475 bytes
コンパイル時間 5,165 ms
コンパイル使用メモリ 263,252 KB
実行使用メモリ 27,304 KB
最終ジャッジ日時 2023-09-20 06:41:05
合計ジャッジ時間 14,855 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 141 ms
14,876 KB
testcase_36 AC 141 ms
21,620 KB
testcase_37 WA -
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 178 ms
21,184 KB
testcase_40 AC 180 ms
21,232 KB
testcase_41 AC 170 ms
21,188 KB
testcase_42 AC 172 ms
21,180 KB
testcase_43 AC 177 ms
21,332 KB
testcase_44 AC 176 ms
21,292 KB
testcase_45 AC 175 ms
21,108 KB
testcase_46 AC 174 ms
21,288 KB
testcase_47 AC 180 ms
21,228 KB
testcase_48 AC 177 ms
21,216 KB
testcase_49 AC 152 ms
27,304 KB
testcase_50 AC 234 ms
14,812 KB
testcase_51 AC 235 ms
14,900 KB
testcase_52 AC 226 ms
14,896 KB
testcase_53 AC 232 ms
14,952 KB
testcase_54 AC 241 ms
14,868 KB
testcase_55 AC 140 ms
21,084 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 136 ms
17,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef tuple<ll, ll, ll> Tll;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const double PI = 3.14159265358979323846;
int ddx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int ddy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

void cans(bool f) {
    if (f)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

template <typename T>
T gcd(T a, T b) {
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}

bool compare1(pair<ll, ll> a, pair<ll, ll> b) { return a.first < b.first; }
bool compare2(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; }

long long modpow(long long a, long long n, long long mod = 998244353) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

template <typename T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;  // aをbで更新
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;  // aをbで更新
        return true;
    }
    return false;
}

using mint = modint998244353;

vector<vector<int>> G;
vector<int> dp;

ll ans = 0;

int dfs(int v, int pv) {
    bool f = true;
    int cnum = 1;
    for (auto nv : G[v]) {
        if (nv == pv) continue;
        cnum += dfs(nv, v);
        f = false;
    }

    if (f) return dp[v] = 1;
    return dp[v] = cnum;
}

int dfs2(int v, int pv) {
    int deg = 0;
    int cnum = 1;
    vector<int> cnt;
    for (auto nv : G[v]) {
        if (nv == pv) continue;
        cnt.push_back(dfs(nv, v));
        deg++;
    }
    sort(all(cnt), greater<int>());
    if (deg == 0) {
        return 1;
    }
    if (deg == 1) {
        return cnt[0] + 1;
    } else {
        return cnt[0] + cnt[1] + 1;
    }
}

int main() {
    int N;
    cin >> N;
    G.resize(N);
    dp.resize(N);
    rep(i, N - 1) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(0, -1);
    // rep(i, N) cout << dp[i] << " ";
    // cout << endl;
    cout << dfs2(0, -1) << endl;
}
0