SPOJ COT2 Count on a tree II

本文介绍了一种解决树上路径查询问题的有效算法——树上莫队算法,并结合离散化与LCA算法进行优化。通过实例展示了算法的具体实现过程及关键细节。

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

You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.


We will ask you to perform the following operation:


u v : ask for how many different integers that represent the weight of nodes there are on the path from u to v.
Input


In the first line there are two integers N and M. (N <= 40000, M <= 100000)


In the second line there are N integers. The i-th integer denotes the weight of the i-th node.


In the next N-1 lines, each line contains two integers u v, which describes an edge (u, v).


In the next M lines, each line contains two integers u v, which means an operation asking for how many different integers that represent the weight of nodes there are on the path from u to v.


Output


For each operation, print its result.


Example


Input:
8 2
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5
7 8
Output:
4

4

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

树上莫队+离散化+lca~

莫队真是太暴力了……

先把值离散化,然后对整棵树分块,块大小sqrt(n),询问离线,用块的顺序排序,每次暴力更新答案即可。

我的WA点:

1.lca里面写成了dep[fa[u][i]]<=dep[v]……果然板子是要多打的啊。

2.fa数组开成了17导致WA到飞起……什么原理?按理来说是算不到17的啊!

3.lca里面dep[1]=0,所以不能用for(int i=17;~i;i--) if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];来去掉dep多出来的值!

(为什么注释掉的那种fa[]更新方式是WA的?求大神指点,感激不尽!)


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int n,m,a[40001],c[40001],pos[40001],fa[40001][18],dep[40001],fi[40001],cnt,tot,jin;
int num[40001],stk[40001],top,dfn[40001],ans[100001],ne[80001],now,tot1,w[80001],cntnum;
bool b[40001];

struct node{
	int x,y,id;
}que[100001];

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

void add(int u,int v)
{
	w[++cnt]=v;ne[cnt]=fi[u];fi[u]=cnt;
	w[++cnt]=u;ne[cnt]=fi[v];fi[v]=cnt;
}

void dfs(int u)
{
	dfn[u]=++cntnum;
	for(int i=1;1<<i<=dep[u];i++) fa[u][i]=fa[fa[u][i-1]][i-1];
	int now=top;
	for(int i=fi[u];i;i=ne[i])
	  if(w[i]!=fa[u][0])
	  {
	  	fa[w[i]][0]=u;dep[w[i]]=dep[u]+1;
	  	dfs(w[i]);
	  	if(top-now>=jin)
	  	{
	  		tot++;
	  		while(top!=now) pos[stk[top--]]=tot;
	  	}
	  }
	stk[++top]=u;
}

bool operator < (node u,node v)
{
	return pos[u.x]==pos[v.x] ? dfn[u.y]<dfn[v.y]:pos[u.x]<pos[v.x];
}

int lca(int u,int v)
{
	if(dep[u]<dep[v]) swap(u,v);
	int now=dep[u]-dep[v];
	for(int i=0;i<=17;i++) if((1<<i)&now) u=fa[u][i];
	if(u==v) return u;
	for(int i=17;~i;i--) if(fa[u][i]!=fa[v][i]) u=fa[u][i],v=fa[v][i];
	return fa[u][0];
}

void ox(int u)
{
	if(b[u]) now-=(--num[a[u]])==0;
	else now+=(++num[a[u]])==1;
	b[u]^=1;
}

void chan(int u,int v)
{
	if(dep[u]<dep[v]) swap(u,v);
	while(dep[u]>dep[v]) ox(u),u=fa[u][0];
	while(u!=v) ox(u),ox(v),u=fa[u][0],v=fa[v][0];
}

int main()
{
	tot1=n=read();m=read();
	for(int i=1;i<=n;i++) a[i]=c[i]=read();
	sort(c+1,c+n+1);
	tot1=unique(c+1,c+n+1)-c-1;
	for(int i=1;i<=n;i++) a[i]=lower_bound(c+1,c+tot1+1,a[i])-c;
	for(int i=1;i<n;i++) add(read(),read());
	jin=sqrt(n);dfs(1);
	while(top) pos[stk[top--]]=tot;
//	for(int i=1;i<=17;i++)
//	  for(int j=1;j<=n;j++) fa[j][i]=fa[fa[j][i-1]][i-1];
	for(int i=1;i<=m;i++)
	{
		int &x=que[i].x,&y=que[i].y;
		x=read(),y=read(),que[i].id=i;
		if(pos[x]>pos[y]) swap(x,y);
	}
	sort(que+1,que+m+1);
	int x=1,y=1;
	for(int i=1;i<=m;i++)
	{
		if(que[i].x!=x) chan(x,que[i].x),x=que[i].x;
		if(que[i].y!=y) chan(y,que[i].y),y=que[i].y;
		ans[que[i].id]=now+(num[a[lca(x,y)]]==0);
	}
	for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值