最近公共祖先

文章提供了三个C++程序,分别实现了洛谷P8805题目的树上点差分、树上边差分以及P3258和P6869题目的类似算法。这些算法主要涉及深度优先搜索(DFS)和最近公共祖先(LCA),用于处理树形结构数据的差分问题。

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

洛谷P8805

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;


//void rfIO()
//{
//	FILE *stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

const int maxn = 1e5 + 7;
int f[maxn][21], v[maxn],n,m,dep[maxn],sum[maxn];
vector<int> g[maxn];

void dfs(int cur, int fa){
	dep[cur] = dep[fa] + 1;
	f[cur][0] = fa;
	for (int i = 1; i <= 20; i++){
		f[cur][i] = f[f[cur][i - 1]][i - 1];
	}
	for (auto &nx : g[cur]){
		if (nx == fa)continue;
		sum[nx] = sum[cur] + v[nx];
		dfs(nx, cur);
	}
}

int lca(int a, int b){
	if (dep[a] > dep[b])swap(a, b);
	for (int i = 20; i >= 0; i--){
		if (dep[a] + (1 << i) <= dep[b]){
			b = f[b][i];
		}
	}
	if (a == b) return a;
	for (int i = 20; i >= 0; i--){
		if (f[a][i] != f[b][i]){
			a = f[a][i];
			b = f[b][i];
		}
	}
	return f[a][0];
}

int main()
{
	cin >> n >> m;
	int a, b;
	for (int i = 1; i < n; i++){
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for (int i = 1; i <= n; i++){
		sum[i] = v[i] = g[i].size();
	}
	dfs(1, 0);
	for (int i = 0; i < m; i++){
		cin >> a >> b;
		int l = lca(a, b);
		int ans = sum[a] + sum[b] - 2 * sum[l] + v[l];
		cout << ans << endl;
	}
	return 0;
}

树上点差分
P3258

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;


inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

const int maxn = 3e5 + 7,LSP = 30;
int n,f[maxn][LSP + 1],dep[maxn], arr[maxn],ans[maxn];
vector<int> g[maxn];

void dfs(int cur, int fa) {
	dep[cur] = dep[fa] + 1;
	f[cur][0] = fa;
	for (int i = 1; i <= LSP; i++) {
		f[cur][i] = f[f[cur][i - 1]][i - 1];
	}

	for (auto& nx : g[cur]) {
		if (nx == fa)continue;
		dfs(nx, cur);
	}
}

int lca(int a, int b) {
	if (dep[a] > dep[b])swap(a, b);
	for (int i = LSP; i >= 0; i--) {
		if (dep[a] + (1 << i) <= dep[b]) {
			b = f[b][i];
		}
	}
	if (a == b)return a;

	for (int i = LSP; i >= 0; i--) {
		if (f[a][i] != f[b][i]) {
			a = f[a][i];
			b = f[b][i];
		}
	}
	return f[a][0];
}
void getSum(int cur, int fa) {
	for (auto& nx : g[cur]) {
		if (nx == fa)continue;
		getSum(nx, cur);
		ans[cur] += ans[nx];
	}
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> arr[i];
	int a, b,c;
	for (int i = 1; i < n; i++) {
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(1, 0);

	for (int i = 1; i < n; i++) {
		a = arr[i], b = arr[i + 1], c = lca(a, b);
		ans[f[c][0]]--;
		ans[c]--;
		ans[a]++;
		ans[b]++;
	}
	getSum(1, 0);
	for (int i = 2; i <= n; i++) {
		ans[arr[i]]--;
	}
	for (int i = 1; i <= n; i++) {
		cout << ans[i] << endl;
	}
	return 0;
}

树上边差分
P6869

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;


//void rfIO()
//{
//	FILE *stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

const int maxn = 2e5 + 7, lg = 31;
int n, f[maxn][lg],val[maxn],dep[maxn];
long long int ans = 0;
struct Node{
	long long int from, to, one, two;
	Node(){ from = to = one = two = 0; }
	Node(int a,int b,int c,int d) :from(a), to(b), one(c), two(d){}
};
vector<Node> g[maxn];

void dfs(int cur, int fa){
	dep[cur] = dep[fa] + 1;
	f[cur][0] = fa;
	for (int i = 1; i < lg; i++){
		f[cur][i] = f[f[cur][i - 1]][i - 1];
	}
	for (auto &p : g[cur]){
		if (p.to == fa)continue;
		dfs(p.to, cur);
	}
}

int lca(int a, int b){
	if (dep[a] > dep[b])swap(a, b);
	for (int i = lg - 1; i >= 0; i--){
		if (dep[a] + (1 << i) <= dep[b]){
			b = f[b][i];
		}
	}
	if (a == b) return a;
	for (int i = lg - 1; i >= 0; i--){
		if (f[a][i] != f[b][i]){
			a = f[a][i];
			b = f[b][i];
		}
	}
	return f[a][0];
}

void slove(int x,int fa)
{
	Node u;
	for (auto &p : g[x]){
		if (p.to == fa){
			u = p;
		}
		else{
			slove(p.to,x);
			val[x] += val[p.to];
		}
	}

	ans += min(u.one*val[x], u.two);
	return;
}

int main()
{
	cin >> n;

	int a, b, c, d;
	for (int i = 1; i < n; i++){
		cin >> a >> b >> c >> d;
		g[a].push_back({ a,b, c, d });
		g[b].push_back({ b,a, c, d });
	}
	dfs(1, 0);
	for (int i = 1; i < n; i++){
		val[i]++; val[i + 1]++;
		val[lca(i, i + 1)] -= 2;
	}
	slove(1,0);
	cout << ans << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值