結果
問題 | No.763 Noelちゃんと木遊び |
ユーザー |
![]() |
提出日時 | 2021-11-18 22:51:26 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 99 ms / 2,000 ms |
コード長 | 1,500 bytes |
コンパイル時間 | 4,305 ms |
コンパイル使用メモリ | 252,996 KB |
実行使用メモリ | 22,784 KB |
最終ジャッジ日時 | 2025-03-22 10:40:24 |
合計ジャッジ時間 | 6,497 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#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; typedef tuple<ll, ll, ll, ll, ll> t5; #define rep(a,n) for(ll a = 0;a < n;a++) template<typename T> static inline void chmin(T& ref, const T value) { if (ref > value) ref = value; } template<typename T> static inline void chmax(T& ref, const T value) { if (ref < value) ref = value; } #include <atcoder/all> using namespace atcoder; typedef modint1000000007 mint; int main() { ll n; 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> dp(n, P{ -1,-1 }); function<P(int, int)> dfs = [&](int current, int parent) { if (dp[current].first != -1) { return dp[current]; } int cs = 0; for (auto x : g[current]) { if (x == parent) continue; cs++;; } if (cs == 0) { dp[current] = P{ 0, 1 }; return dp[current]; } ll cut = 0; for (auto x : g[current]) { if (x == parent) continue; P p = dfs(x, current); cut += max(p.first, p.second); } ll notCut = 1; for (auto x : g[current]) { if (x == parent) continue; P p = dfs(x, current); notCut += max(p.first, p.second - 1); } dp[current] = { cut, notCut }; return dp[current]; }; P v = dfs(0, -1); cout << max(v.first, v.second) << endl; return 0; }