結果

問題 No.763 Noelちゃんと木遊び
ユーザー zawakasuzawakasu
提出日時 2023-01-31 10:53:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 3,346 bytes
コンパイル時間 2,685 ms
コンパイル使用メモリ 205,288 KB
実行使用メモリ 21,764 KB
最終ジャッジ日時 2023-09-13 05:05:13
合計ジャッジ時間 5,498 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
21,764 KB
testcase_01 AC 18 ms
6,692 KB
testcase_02 AC 52 ms
11,476 KB
testcase_03 AC 30 ms
8,760 KB
testcase_04 AC 21 ms
7,244 KB
testcase_05 AC 30 ms
8,500 KB
testcase_06 AC 67 ms
13,368 KB
testcase_07 AC 68 ms
13,248 KB
testcase_08 AC 33 ms
9,136 KB
testcase_09 AC 20 ms
6,952 KB
testcase_10 AC 9 ms
4,560 KB
testcase_11 AC 71 ms
13,648 KB
testcase_12 AC 58 ms
12,500 KB
testcase_13 AC 57 ms
12,212 KB
testcase_14 AC 50 ms
11,380 KB
testcase_15 AC 31 ms
8,804 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 29 ms
8,884 KB
testcase_18 AC 73 ms
13,568 KB
testcase_19 AC 64 ms
12,492 KB
testcase_20 AC 62 ms
12,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define all(x) begin(x), end(x)
#define times(x) for (int _ = 0 ; _ < (int)(x) ; _++)

using i32 = int;
using i64 = long long;
using ld = long double;
using usize = std::size_t;

template <class T1, class T2>
inline bool chmax(T1 &a, const T2 &b) { return a < b and (a = b, true); }
template <class T1, class T2>
inline bool chmin(T1 &a, const T2 &b) { return a > b and (a = b, true); }

constexpr i64 supl = (std::numeric_limits<i64>::max() >> 1) - 100;
constexpr i32 supi = (std::numeric_limits<i32>::max() >> 1) - 100;



namespace zawa::input {

template <typename T> 
void in(T& res) { std::cin >> res; }

template <typename Head, typename... Tail>
void in(Head& head, Tail&... tail) { in(head); in(tail...); }

template <typename T, typename U>
void in(std::pair<T, U>& res) { in(res.first); in(res.second); }

template <typename T>
void in(std::vector<T>& res) { for (auto& r : res) { in(r); } }

} // namespace zawa::input
using zawa::input::in;



namespace zawa::output {

void out() { 
    std::cout << std::endl; 
}

template <class T>
void out(const T& a) { 
    std::cout << a << std::endl; 
}

template <class T>
void out(const std::vector<T>& as) { 
    for (std::size_t i = 0 ; i < as.size() ; i++) { 
        std::cout << as[i] << (i + 1 == as.size() ? '\n' : ' '); 
    } 
}

template <class Head, class... Tail>
void out(const Head& head, const Tail&... tail) { 
    std::cout << head; 
    if (sizeof...(tail)) { 
        std::cout << ' '; 
    } 
    out(tail...);
}

void yesno(bool flag, std::string yes = "Yes", std::string no = "No") {
    std::cout << (flag ? yes : no) << std::endl;
}

} // namespace zawa::output
using zawa::output::out;
using zawa::output::yesno;

// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
// using mint = atcoder::modint1000000007;

// #include "src/template/accum1d.hpp"
// #include "src/template/binary-search.hpp"
// #include "src/template/binary-search-ld.hpp"
// #include "src/algorithm/compression.hpp"
// #include "src/algorithm/RLE.hpp"



namespace zawa {

std::vector<std::vector<int>> read_graph(int n, int m, bool undirect = true, bool minus = true) {
    std::vector<std::vector<int>> res(n, std::vector(0, 0));
    for (int _ = 0 ; _ < m ; _++) {
        int u, v;
        std::cin >> u >> v;
        res[u - minus].emplace_back(v - minus);
        if (undirect) {
            res[v - minus].emplace_back(u - minus);
        }
    }
    return res;
}

std::vector<std::vector<int>> read_tree(int n, bool undirect = true, bool minus = true) {
    return read_graph(n, n - 1, undirect, minus);
}

}
// #include "src/graph/Read-Weighted-Graph.hpp"

using namespace std;

void main_() {
	i32 n; in(n);
	auto G = zawa::read_tree(n);
	vector dp(n, vector(2, 0));
	auto rec = [&](auto rec, i32 v, i32 p) -> void {
		for (usize i = 0 ; i < G[v].size() ; i++) {
			if (G[v][i] == p) {
				G[v].erase(G[v].begin() + i);
				break;
			}
		}
		for (auto x : G[v]) {
			rec(rec, x, v);
		}
		dp[v][0] = 0;
		for (auto x : G[v]) {
			dp[v][0] += max(dp[x][0], dp[x][1]);
		}
		dp[v][1] = 1;
		for (auto x : G[v]) {
			dp[v][1] += max(dp[x][0], dp[x][1] - 1);
		}
	};
	rec(rec, 0, -1);
	out(*max_element(all(dp[0])));
}

i32 main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(10);

	main_();

	return 0;
}
0