点分治学习笔记

1多用于处理树上规模较大的路径信息问题

2对于一棵以root为根节点的树,存在两种路径,一种是经过root的路径,一种是不经过root的路径

3因此对于一棵以root为根节点的树,只去考虑经过当前根节点root的路径,又可以分成两种路径,一种为有一端是根节点的路径,也就是root向下遍历形成的一条链,另一种是根节点在路径中而不在两端的路径,这种路径能够通过两条不同的第一种路径合并得到。

4处理外以root为根节点的树后,割去root,形成了多棵子树,再次设定根节点,根据3进行分类讨论计算答案。

5每次选取树的重心为根节点能使递归层数更少,从而降低时间复杂度。

代码实现:

根据模板题:【模板】点分治1 - 洛谷

1数据存储

const int maxn=1e7+10;
int root,siz[maxn],sum;//记录根节点,节点子树大小,当前树的大小 
bool vis[maxn];//标记当前点被割掉 
int q[maxn];//询问的距离 
int n,m;
int dis[maxn];//记录当前节点和根节点的距离 
int cnt,temp[maxn];//暂时保存当前节点与根节点这条路径的长度,便于结算答案 
int judge[maxn];//标记距离为w的路径存在 
bool ans[maxn];//记录第i次询问的答案 
struct edge{
	int to,w;
};
vector<edge>vv[maxn];

2获取树的重心

void getroot(int u,int fa)//获取树的重心 
{
	siz[u]=1;
	int mx=0;//最大的子树 
	for(edge e:vv[u])
	{
		int to=e.to;
		if(to==fa||vis[to])
		continue;
		getroot(to,u);
		siz[u]+=siz[to];
		mx=max(siz[to],mx);//寻找最大子树 
	}
	mx=max(mx,sum-siz[u]);//当前节点父节点方向的树也算子树 
	if(mx*2<=sum)
	root=u;
}

3获取当前子树每个节点到当前根节点的距离

void getdis(int u,int fa)//计算当前树中所有节点和树的根节点的距离 
{
	temp[++cnt]=dis[u];
	for(edge e:vv[u])
	{
		int to=e.to;
		int w=e.w;
		if(to==fa||vis[to])
		continue;
		dis[to]=dis[u]+w;
		getdis(to,u);
	}
}

4对于当前的树,结算当前经过根节点的路径

void solve(int u)//结算当前树的答案 
{
	queue<int>que;//用队列记录长度,方便清空judge标记,(应该也可以用栈,可能更快) 
	for(edge e:vv[u])
	{
		int to=e.to;
		int w=e.w;
		if(vis[to])
		continue;
		cnt=0;
		dis[to]=w;
		getdis(to,u);
		for(int j=1;j<=cnt;j++)/*这次获取的长度在前不用judge标记,在当前子节点的子树中找到重复的路径组成询问的
		长度,实际上两条重复的路径结合成的路径是不合理的*/ 
		for(int i=1;i<=m;i++)
		if(q[i]>=temp[j])//防止数组越界 
		ans[i]|=judge[q[i]-temp[j]];//对于一个长为temp[j],寻找是否存在一条q[i]-temp[j]的路径,若有,则ans[i]=1 
		for(int i=1;i<=cnt;i++)//用judge标记 
		{
			que.push(temp[i]);
			judge[temp[i]]=1;
		}
	}
		while(!que.empty())
		{
			int p=que.front();
			que.pop();
			judge[p]=0;
		}
}

5分治操作

void divide(int u)//分治操作 
{
	vis[u]=judge[0]=1;//标记当前点被割掉了,并且标记长度为0的路径存在 
	solve(u);//寻找经过u点的答案 
	for(edge e:vv[u])
	{
		int to=e.to;
		if(vis[to])
		continue;
		sum=siz[to];
		getroot(to,0);//寻找树的重心作为根节点 
		getroot(root,0);//当重心作为根节点是,计算每个节点的子树大小 
		divide(root);
	}
}

6主函数

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	for(int i=1;i<=m;i++)
	cin>>q[i];
	sum=n;
	getroot(1,0);
	getroot(root,0);
	divide(root);
	for(int i=1;i<=m;i++)
	if(ans[i])
	cout<<"AYE\n";
	else
	cout<<"NAY\n";
	return 0;
}

7完整代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e7+10;
int root,siz[maxn],sum;//记录根节点,节点子树大小,当前树的大小 
bool vis[maxn];//标记当前点被割掉 
int q[maxn];//询问的距离 
int n,m;
int dis[maxn];//记录当前节点和根节点的距离 
int cnt,temp[maxn];//暂时保存当前节点与根节点这条路径的长度,便于结算答案 
int judge[maxn];//标记距离为w的路径存在 
bool ans[maxn];//记录第i次询问的答案 
struct edge{
	int to,w;
};
vector<edge>vv[maxn];
inline void add(int u,int v,int w)
{
	vv[u].push_back({v,w});
	vv[v].push_back({u,w});
}
void getroot(int u,int fa)//获取树的重心 
{
	siz[u]=1;
	int mx=0;//最大的子树 
	for(edge e:vv[u])
	{
		int to=e.to;
		if(to==fa||vis[to])
		continue;
		getroot(to,u);
		siz[u]+=siz[to];
		mx=max(siz[to],mx);//寻找最大子树 
	}
	mx=max(mx,sum-siz[u]);//当前节点父节点方向的树也算子树 
	if(mx*2<=sum)
	root=u;
}
void getdis(int u,int fa)//计算当前树中所有节点和树的根节点的距离 
{
	temp[++cnt]=dis[u];
	for(edge e:vv[u])
	{
		int to=e.to;
		int w=e.w;
		if(to==fa||vis[to])
		continue;
		dis[to]=dis[u]+w;
		getdis(to,u);
	}
}
void solve(int u)//结算当前树的答案 
{
	queue<int>que;//用队列记录长度,方便清空judge标记,(应该也可以用栈,可能更快) 
	for(edge e:vv[u])
	{
		int to=e.to;
		int w=e.w;
		if(vis[to])
		continue;
		cnt=0;
		dis[to]=w;
		getdis(to,u);
		for(int j=1;j<=cnt;j++)/*这次获取的长度在前不用judge标记,在当前子节点的子树中找到重复的路径组成询问的
		长度,实际上两条重复的路径结合成的路径是不合理的*/ 
		for(int i=1;i<=m;i++)
		if(q[i]>=temp[j])//防止数组越界 
		ans[i]|=judge[q[i]-temp[j]];//对于一个长为temp[j],寻找是否存在一条q[i]-temp[j]的路径,若有,则ans[i]=1 
		for(int i=1;i<=cnt;i++)//用judge标记 
		{
			que.push(temp[i]);
			judge[temp[i]]=1;
		}
	}
		while(!que.empty())
		{
			int p=que.front();
			que.pop();
			judge[p]=0;
		}
}
void divide(int u)//分治操作 
{
	vis[u]=judge[0]=1;//标记当前点被割掉了,并且标记长度为0的路径存在 
	solve(u);//寻找经过u点的答案 
	for(edge e:vv[u])
	{
		int to=e.to;
		if(vis[to])
		continue;
		sum=siz[to];
		getroot(to,0);//寻找树的重心作为根节点 
		getroot(root,0);//当重心作为根节点是,计算每个节点的子树大小 
		divide(root);
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	for(int i=1;i<=m;i++)
	cin>>q[i];
	sum=n;
	getroot(1,0);
	getroot(root,0);
	divide(root);
	for(int i=1;i<=m;i++)
	if(ans[i])
	cout<<"AYE\n";
	else
	cout<<"NAY\n";
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值