結果

問題 No.2677 Minmax Independent Set
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-15 22:59:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 3,973 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 136,624 KB
実行使用メモリ 46,960 KB
最終ジャッジ日時 2024-03-15 22:59:19
合計ジャッジ時間 10,250 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 200 ms
27,248 KB
testcase_06 AC 197 ms
27,632 KB
testcase_07 AC 193 ms
27,760 KB
testcase_08 AC 194 ms
28,272 KB
testcase_09 AC 194 ms
29,296 KB
testcase_10 AC 197 ms
25,772 KB
testcase_11 AC 170 ms
25,772 KB
testcase_12 AC 192 ms
46,960 KB
testcase_13 AC 219 ms
32,920 KB
testcase_14 AC 206 ms
32,888 KB
testcase_15 AC 234 ms
37,360 KB
testcase_16 AC 223 ms
25,292 KB
testcase_17 AC 225 ms
25,292 KB
testcase_18 AC 128 ms
17,376 KB
testcase_19 AC 155 ms
19,908 KB
testcase_20 AC 38 ms
9,088 KB
testcase_21 AC 175 ms
21,664 KB
testcase_22 AC 10 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 182 ms
25,852 KB
testcase_29 AC 212 ms
38,868 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 3 ms
6,676 KB
testcase_39 AC 2 ms
6,676 KB
testcase_40 AC 3 ms
6,676 KB
testcase_41 AC 5 ms
6,676 KB
testcase_42 AC 9 ms
6,676 KB
testcase_43 AC 16 ms
6,676 KB
testcase_44 AC 35 ms
8,704 KB
testcase_45 AC 98 ms
14,080 KB
testcase_46 AC 221 ms
24,976 KB
testcase_47 AC 261 ms
25,260 KB
testcase_48 AC 222 ms
25,260 KB
testcase_49 AC 220 ms
25,260 KB
testcase_50 AC 47 ms
11,860 KB
testcase_51 AC 4 ms
6,676 KB
testcase_52 AC 134 ms
22,036 KB
testcase_53 AC 122 ms
20,580 KB
testcase_54 AC 4 ms
6,676 KB
testcase_55 AC 213 ms
25,924 KB
testcase_56 AC 179 ms
25,928 KB
testcase_57 AC 183 ms
25,908 KB
testcase_58 AC 187 ms
25,884 KB
testcase_59 AC 179 ms
25,876 KB
testcase_60 AC 85 ms
25,224 KB
testcase_61 AC 93 ms
25,492 KB
testcase_62 AC 93 ms
29,424 KB
testcase_63 AC 109 ms
36,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;

#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;
struct UnionFind
{
	private:
	int n;
	vector<int> par,siz;

	public:
	UnionFind(int n) :n(n),par(n,-1),siz(n,1) {}

	int root(int u) 
	{
		assert(0 <= u && u < n);
		return (par[u] < 0 ? u:par[u] = root(par[u]));
	}

	bool same(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		return root(u) == root(v);
	}

	bool unite(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		u = root(u),v = root(v);
		if(u == v) return false;

		if(siz[u] < siz[v]) swap(u,v);

		siz[u] += siz[v];
		par[v] = u;

		return true;
	}

	int size(int u)
	{
		assert(0 <= u && u < n);
		return siz[root(u)];
	}

	vector<vector<int>> components()
	{
		vector<vector<int>> ret(n);
		for(int u = 0;u < n;u++) ret[root(u)].push_back(u);

		ret.erase(remove_if(ret.begin(),ret.end(),[](vector<int> v) { return v.empty();}),ret.end());

		return ret;
	}
};

void Main()
{
	int N;
	cin >> N;
	vector<vector<int>> G(N);
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		u--; v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	vector<vector<int>> dp(N,vector<int>(2));

	auto dfs0 = [&](auto dfs,int u,int p) -> void
	{
		dp[u][1] = 1;
		for(int v : G[u])
		{
			if(v == p)
			{
				continue;
			}
			dfs(dfs,v,u);
			dp[u][0] += max(dp[v][0],dp[v][1]);
			dp[u][1] += dp[v][0];
		}
	};

	auto dfs1 = [&](auto dfs,int u,int p) -> void
	{
		if(p != -1)
		{
			vector<int> pdp(dp[p].begin(),dp[p].end());
			pdp[0] -= max(dp[u][0],dp[u][1]);
			pdp[1] -= dp[u][0];
			dp[u][0] += max(pdp[0],pdp[1]);
			dp[u][1] += pdp[0];
		}
		for(int v : G[u])
		{
			if(v == p)
			{
				continue;
			}
			dfs(dfs,v,u);
		}
	};
	dfs0(dfs0,0,-1);
	dfs1(dfs1,0,-1);
	int ans = (int)1e9;
	for(int i = 0;i < N;i++)
	{
		ans = min(ans,dp[i][1]);
	}
	cout << ans << "\n";
	/* int N,M; */
	/* cin >> N >> M; */
	/* int s,t,k; */
	/* cin >> s >> t >> k; */
	/* s--; t--; */
	/* vector<vector<int>> G(N); */
	/* UnionFind uf(N); */
	/* for(int i = 0;i < M;i++) */
	/* { */
	/* 	int u,v; */
	/* 	cin >> u >> v; */
	/* 	u--; v--; */
	/* 	G[u].push_back(v); */
	/* 	G[v].push_back(u); */
	/* 	uf.unite(u,v); */
	/* } */

	/* if(uf.same(s,t)) */
	/* { */
	/* 	vector<int> C(N,-1); */
	/* 	auto dfs = [&](auto dfs,int u,int c) -> void */
	/* 	{ */
	/* 		C[u] = c; */
	/* 		for(int v : G[u]) */
	/* 		{ */
	/* 			if(C[v] == -1) */
	/* 			{ */
	/* 				dfs(dfs,v,1 - c); */
	/* 			} */
	/* 		} */
	/* 	}; */
	/* 	dfs(dfs,s,0); */
	/* 	assert(C[t] != -1); */
	/* 	if(C[s] == C[t] && k % 2 == 1) */
	/* 	{ */
	/* 		cout << "No\n"; */
	/* 		return; */
	/* 	} */
	/* 	if(C[s] != C[t] && k % 2 == 0) */
	/* 	{ */
	/* 		cout << "No\n"; */
	/* 		return; */ 
	/* 	} */
	/* 	if(s == t && uf.size(s) == 1) */
	/* 	{ */
	/* 		cout << "Unknown\n"; */
	/* 		return; */
	/* 	} */
	/* 	vector<int> dp(N,-1); */
	/* 	dp[s] = 0; */
	/* 	queue<int> Q; */
	/* 	Q.push(s); */
	/* 	while(!Q.empty()) */
	/* 	{ */
	/* 		int u = Q.front(); */
	/* 		Q.pop(); */
	/* 		for(int v : G[u]) */
	/* 		{ */
	/* 			if(dp[v] == -1) */
	/* 			{ */
	/* 				dp[v] = dp[u] + 1; */
	/* 				Q.push(v); */
	/* 			} */
	/* 		} */
	/* 	} */
	/* 	assert(dp[t] % 2 == k % 2); */
	/* 	if(dp[t] <= k) */
	/* 	{ */
	/* 		cout << "Yes\n"; */
	/* 	} */
	/* 	else */
	/* 	{ */
	/* 		cout << "Unknown\n"; */
	/* 	} */
	/* } */
	/* else */
	/* { */
	/* 	cout << "Unknown\n"; */
	/* } */
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}

0