結果

問題 No.2677 Minmax Independent Set
ユーザー shobonvipshobonvip
提出日時 2024-03-15 22:20:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 2,440 bytes
コンパイル時間 4,960 ms
コンパイル使用メモリ 265,440 KB
実行使用メモリ 79,172 KB
最終ジャッジ日時 2024-03-15 22:21:14
合計ジャッジ時間 13,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 200 ms
22,980 KB
testcase_06 AC 195 ms
23,748 KB
testcase_07 AC 193 ms
24,388 KB
testcase_08 AC 194 ms
25,796 KB
testcase_09 AC 198 ms
28,612 KB
testcase_10 AC 144 ms
19,700 KB
testcase_11 AC 141 ms
19,704 KB
testcase_12 AC 272 ms
79,172 KB
testcase_13 AC 264 ms
39,412 KB
testcase_14 AC 204 ms
39,096 KB
testcase_15 AC 278 ms
51,976 KB
testcase_16 AC 223 ms
16,836 KB
testcase_17 AC 248 ms
16,836 KB
testcase_18 AC 130 ms
12,032 KB
testcase_19 AC 156 ms
13,508 KB
testcase_20 AC 42 ms
7,040 KB
testcase_21 AC 176 ms
14,660 KB
testcase_22 AC 10 ms
6,548 KB
testcase_23 AC 2 ms
6,548 KB
testcase_24 AC 2 ms
6,548 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 2 ms
6,548 KB
testcase_27 AC 2 ms
6,548 KB
testcase_28 AC 136 ms
19,892 KB
testcase_29 AC 243 ms
56,128 KB
testcase_30 AC 2 ms
6,548 KB
testcase_31 AC 2 ms
6,548 KB
testcase_32 AC 2 ms
6,548 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 2 ms
6,548 KB
testcase_35 AC 2 ms
6,548 KB
testcase_36 AC 2 ms
6,548 KB
testcase_37 AC 2 ms
6,548 KB
testcase_38 AC 2 ms
6,548 KB
testcase_39 AC 2 ms
6,548 KB
testcase_40 AC 4 ms
6,548 KB
testcase_41 AC 5 ms
6,548 KB
testcase_42 AC 9 ms
6,548 KB
testcase_43 AC 17 ms
6,548 KB
testcase_44 AC 38 ms
6,912 KB
testcase_45 AC 94 ms
10,240 KB
testcase_46 AC 223 ms
16,888 KB
testcase_47 AC 223 ms
16,964 KB
testcase_48 AC 217 ms
16,964 KB
testcase_49 AC 219 ms
17,092 KB
testcase_50 AC 44 ms
9,812 KB
testcase_51 AC 5 ms
6,548 KB
testcase_52 AC 112 ms
17,428 KB
testcase_53 AC 94 ms
16,484 KB
testcase_54 AC 4 ms
6,548 KB
testcase_55 AC 151 ms
19,112 KB
testcase_56 AC 150 ms
19,300 KB
testcase_57 AC 152 ms
19,148 KB
testcase_58 AC 159 ms
19,120 KB
testcase_59 AC 176 ms
19,372 KB
testcase_60 AC 90 ms
17,348 KB
testcase_61 AC 95 ms
17,476 KB
testcase_62 AC 105 ms
29,124 KB
testcase_63 AC 125 ms
49,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n; cin >> n;
	vector ikeru(n, vector<int>(0));
	rep(i,0,n-1){
		int u, v; cin >> u >> v;
		u--; v--;
		ikeru[u].push_back(v);
		ikeru[v].push_back(u);
	}

	vector<int> dp0(n);
	vector<int> dp1(n);

	auto dfs1 = [&](auto self, int i, int p) -> void {
		dp0[i] = 0;
		dp1[i] = 1;
		for (int j: ikeru[i]){
			if (j == p) continue;
			self(self, j, i);
			dp0[i] += max(dp1[j], dp0[j]);

			dp1[i] += dp0[j];
		}
		return;
	};

	dfs1(dfs1, 0, -1);

	vector<int> ans(n);
	auto dfs2 = [&](auto self, int i, int p, int up0, int up1) -> void {
		vector<int> rui_left0 = {0};
		vector<int> rui_left1 = {0};
		int now_left0 = 0;
		int now_left1 = 0;
		int cnt = 0;
		for (int j: ikeru[i]){
			if (j == p) continue;
			now_left0 += max(dp0[j], dp1[j]);
			now_left1 += dp0[j];
			cnt++;
			rui_left0.push_back(now_left0);
			rui_left1.push_back(now_left1);
		}

		reverse(ikeru[i].begin(), ikeru[i].end());

		now_left0 = 0;
		now_left1 = 0;
		for (int j: ikeru[i]){
			if (j == p) continue;
			cnt--;

			self(self, j, i,
				max(up0, up1) + rui_left0[cnt] + now_left0,
				up0 + rui_left1[cnt] + now_left1 + 1
			);

			now_left0 += max(dp0[j], dp1[j]);
			now_left1 += dp0[j];
		}

		ans[i] = now_left1 + up0 + 1;
	};

	dfs2(dfs2, 0, -1, 0, 0);
	
	/*
	rep(i,0,n){
		cout << ans[i] << ' ';
	}cout << '\n';
	*/

	cout << min(ans) << '\n';
}

0