POJ 3321 Apple Tree

这篇博客探讨了一种处理树结构的算法问题,涉及单点修改和子节点查询。博主首先通过深度优先搜索(DFS)为树的每个节点重新编号,并确定节点及其子节点的范围。接着,利用线段树的数据结构对节点数量进行初始化和维护。在修改操作中,使用异或操作更新节点计数。最后,展示了C++代码实现,虽然POJ平台不支持新版本C++,但博主在本地验证了代码的正确性。
摘要由CSDN通过智能技术生成

Apple Tree

POJ - 3321

因POJ不支持C++的新版本,因此没提交,自己造的数据都通过了。

先说题意,给你一颗树(没说二叉树),让你进行单点修改或某点的子节点数量查询。

思路:先dfs,对每个结点重新标号,然后标记其本身与子节点的范围。随后对每个节点的数量初始化为其节点范围大小。然后就可对其进行线段树的修改维护与查询了。修改时可以用异或。

代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
#include<vector>

using namespace std;

int n, m;
int a, b, idx;
char ch;

struct P {
	bool val;
	int cnt;
	int l, r;
	vector<int>v;
}tre[100005];

void dfs(int p) {
	tre[p].l = ++idx;
	for (auto x : tre[p].v) dfs(x);
	tre[p].r = idx;
}

int built(int p,int l,int r) {
	if (l==r)return tre[p].cnt = 1;
	for (auto x : tre[p].v) built(x,tre[x].l,tre[x].r);
	tre[p].cnt = tre[p].r - tre[p].l + 1;
}

int modify(int p, int key) {
	if (p == key) {
		if (tre[p].val)tre[p].cnt--;
		else tre[p].cnt++;
		return (tre[p].val ^= 1);
	}
	int ans;
	for (auto x : tre[p].v) {
		if (key >= tre[p].l && key <= tre[p].r) {
			ans = modify(x, key);
			if (ans) tre[p].cnt++;
			else tre[p].cnt--;
			return ans;
		}
	}
}

int findkey(int p, int key) {
	if (p == key)return tre[p].cnt;
	for (auto x : tre[p].v)
		if (key >= tre[x].l && key <= tre[x].r) return findkey(x, key);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) tre[i].val = true;
	for (int i = 1; i < n; i++){
		cin >> a >> b;
		tre[a].v.push_back(b);
	}
	dfs(1); built(1, 1, n);
	cin >> m;
	while (m--) {
		cin >> ch >> a;
		if (ch == 'C')modify(1, a);
		else cout << findkey(1, a) << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值