CF1375G. Tree Modification(贪心,黑白染色)

这篇博客讨论了树的结构在进行特定修改时的最优策略。作者指出,选取根节点后,总是从深度较大的节点连接到深度较小的节点是可行的。操作次数等于偶数深度节点数量减一。博客还提到,奇数深度和偶数深度节点数量较小值减一即为最终操作数。代码实现中展示了如何进行这样的计算,并提供了DFS遍历和计数的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CF1375G. Tree Modification

Solution

假设我们取定了根,那么只可能从深度大的点接到深度小的点,我们每次取一个高度为2的子树接到该子树的父亲,这样取一定不劣,操作次数相当于是偶数深度点(根深度为0)的个数减一。

注意到所有奇数深度的点为根的答案都相同,偶数同理。
因此整合之后,相当于是奇数深度和偶数深度点的个数的较小值减一。

时间复杂度 O ( n ) O(n) O(n)

Code

#include <bits/stdc++.h>
 
using namespace std;
 
template<typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; }
template<typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
 
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI; 
 
const lod eps = 1e-9;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 60;
const int mods = 1e9 + 7;
const int inv2 = (mods + 1) >> 1;
const int MAXN = 500005;
const int INF = 0x3f3f3f3f; //1061109567
/*--------------------------------------------------------------------*/
 
namespace FastIO{
	constexpr int SIZE = (1 << 21) + 1;
	int num = 0, f;
	char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c;
	#define gc() (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS ++)) : *iS ++)
	inline void flush() {
		fwrite(obuf, 1, oS - obuf, stdout);
		oS = obuf;
	}
	inline void putc(char c) {
		*oS ++ = c;
		if (oS == oT) flush();
	}
	inline void getc(char &c) {
		for (c = gc(); c != '<' && c != '>' && c != EOF; c = gc());
	}
	inline void reads(char *st) {
		char c;
		int n = 0;
		getc(st[++ n]);
		for (c = gc(); c == '<' || c == '>' ; c = gc()) st[++ n] = c;
		st[n + 1] = '\0';
	}
	template<class I>
	inline void read(I &x) {
		for (f = 1, c = gc(); c < '0' || c > '9' ; c = gc()) if (c == '-') f = -1;
		for (x = 0; c >= '0' && c <= '9' ; c = gc()) x = (x << 3) + (x << 1) + (c & 15);
		x *= f;
	}
	template<class I>
	inline void print(I x) {
		if (x < 0) putc('-'), x = -x;
		if (!x) putc('0');
		while (x) que[++ num] = x % 10 + 48, x /= 10;
		while (num) putc(que[num --]);
	}
	struct Flusher_{~Flusher_(){flush();}} io_Flusher_;
}
using FastIO :: read;
using FastIO :: putc;
using FastIO :: reads;
using FastIO :: print;
 
 
 
 
vector<int> e[MAXN];
int dep[MAXN], num[2];
void dfs(int x, int father) {
	dep[x] = dep[father] + 1;
	++ num[dep[x] & 1];
	for (auto v : e[x]) {
		if (v == father) continue;
		dfs(v, x);
	}
}
signed main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
#endif
	int n;
	read(n);
	for (int i = 1, u, v; i < n ; ++ i) read(u), read(v), e[u].PB(v), e[v].PB(u); 
	dfs(1, 0);
	print(min(num[0], num[1]) - 1);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值