COT2 - Count on a tree II

http://www.spoj.com/problems/COT2/

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 tov.

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
 

题意:问树上两点间有多少不同的权值

树上莫队

开始狂T,发现自己竟是按节点编号划分的块!!

dfs分块。。

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 40001
#define M 100001
int n,m,siz,tmp;
int hash[N],key[N];
int front[N],to[N*],nxt[N*],tot;
int fa[N],deep[N],id[N],son[N],bl[N],block[N];
bool vis[N];
int sum[N],ans[M];
struct node
{
int l,r,id;
bool operator < (node p) const
{
if(block[l]!=block[p.l]) return block[l]<block[p.l];
return block[r]<block[p.r];
}
}e[M];
int read(int &x)
{
x=; char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') { x=x*+c-''; c=getchar(); }
}
void add(int x,int y)
{
to[++tot]=y; nxt[tot]=front[x]; front[x]=tot;
to[++tot]=x; nxt[tot]=front[y]; front[y]=tot;
}
void dfs(int x)
{
son[x]++;
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==fa[x]) continue;
deep[to[i]]=deep[x]+;
fa[to[i]]=x;
dfs(to[i]);
son[x]+=son[to[i]];
}
}
void dfs2(int x,int top)
{
id[x]=++tot;
bl[x]=top;
block[x]=(tot-)/siz+;
int y=;
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==fa[x]) continue;
if(son[to[i]]>son[y]) y=to[i];
}
if(!y) return;
dfs2(y,top);
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==fa[x]||to[i]==y) continue;
dfs2(to[i],to[i]);
}
}
void point(int u)
{
if(vis[u]) tmp-=(!--sum[hash[u]]);
else tmp+=(++sum[hash[u]]==);
vis[u]^=;
}
void path(int u,int v)
{
while(u!=v)
{
if(deep[u]>deep[v]) point(u),u=fa[u];
else point(v),v=fa[v];
}
}
int get_lca(int u,int v)
{
while(bl[u]!=bl[v])
{
if(deep[bl[u]]<deep[bl[v]]) swap(u,v);
u=fa[bl[u]];
}
return deep[u]<deep[v] ? u : v;
}
int main()
{
read(n);read(m); siz=sqrt(n);
for(int i=;i<=n;i++) read(key[i]),hash[i]=key[i];
sort(key+,key+n+);
key[]=unique(key+,key+n+)-(key+);
for(int i=;i<=n;i++) hash[i]=lower_bound(key+,key+key[]+,hash[i])-key;
int x,y;
for(int i=;i<n;i++)
{
read(x); read(y);
add(x,y);
}
tot=;
dfs();
dfs2(,);
for(int i=;i<=m;i++)
{
read(e[i].l); read(e[i].r);
e[i].id=i;
}
sort(e+,e+m+);
int L=,R=,lca;
for(int i=;i<=m;i++)
{
if(id[e[i].l]>id[e[i].r]) swap(e[i].l,e[i].r);
path(L,e[i].l);
path(R,e[i].r);
lca=get_lca(e[i].l,e[i].r);
point(lca);
ans[e[i].id]=tmp;
point(lca);
L=e[i].l; R=e[i].r;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
}
 

spoj COT2 - Count on a tree II的更多相关文章

  1. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  2. SPOJ COT2 Count on a tree II(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

  3. SPOJ COT2 Count on a tree II (树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

  4. spoj COT2 - Count on a tree II 树上莫队

    题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的,  受益匪浅.. #include <iostream> #include < ...

  5. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

  6. SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)

    题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...

  7. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  8. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  9. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

随机推荐

  1. 深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)

    转载:http://zh.lucida.me/blog/java-8-lambdas-inside-out-library-features/ 关于 深入理解 Java 8 Lambda(语言篇——l ...

  2. Rescue(BFS时间最短 另开数组或优先队列)

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  3. 全排列 next_permutation() 函数的用法

    在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...

  4. 【Docker 命令】- build命令

    docker build 命令用于使用 Dockerfile 创建镜像. 语法 docker build [OPTIONS] PATH | URL | - OPTIONS说明: --build-arg ...

  5. IIS发布 MVC 配置

    E:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll

  6. AutoResetEvent的基本用法

    The following example uses an AutoResetEvent to synchronize the activities of two threads.The first ...

  7. C#中Console.ReadLine()和Console.Read()有何区别?

    Console.Read 表示从控制台读取字符串,不换行. Console.ReadLine 表示从控制台读取字符串后进行换行. Console.Read() Console.ReadLine()方法 ...

  8. dev_queue_xmit 发生了什么?skb还会在哪里缓存

    见 codebox/net/qdisk/xmit.log中保存了一份记录 调用关系 sch_direct_xmit --> dev_hard_start_xmit --> xmit_one ...

  9. Android Studio类中实现Serializable自动生成serialVersionUID

    1.File -> Settings... -> Editor -> Inspections -> Serialization issues -> Serializabl ...

  10. 【bzoj1712】[Usaco2007 China]Summing Sums 加密 矩阵乘法

    题目描述 那N只可爱的奶牛刚刚学习了有关密码的许多算法,终于,她们创造出了属于奶牛的加密方法.由于她们并不是经验十足,她们的加密方法非常简单:第i只奶牛掌握着密码的第i个数字,起始的时候是Ci(0≤C ...