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. 浅谈CPU、内存、硬盘之间的关系

    计算机,大家都知道的,就是我们日常用的电脑,不管台式的还是笔记本都是计算机.那么这个看着很复杂的机器由哪些组成的呢,今天就简单的来了解一下. 先放图: 图上展示的就是计算机的基本组成啦. 首先是输入设 ...

  2. 最小生成树——prim

    prim:逐“点”生成最小生成树 与Dijkstra不同的是:加入点到生成树中,不要考虑与源点的距离,而是考虑与生成树的距离 #include <iostream> #include &l ...

  3. 路由器如何设置上网(TP-LINK)

    最近宿舍公用的网络一直不太稳定,正赶上毕业季,本来就打算自己买一台自用的路由器,于是我从一个毕业的师姐手里15RMB收了一台路由器,师姐还给了我一根5m的网线和两根全新15m的,感觉光网线就赚翻了. ...

  4. EasyJSWebView原理分析

    概述 在iOS6之前,native只能调用webiew里的js代码,官方没有提供js调用native方法的接口.到了iOS7,官方提供了JSContext用来与js交互,native和js可以双向调用 ...

  5. LintCode-41.最大子数组

    最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2 ...

  6. TCP系列37—Keep Alive—1、TCP存活检测

    一.TCP存活(keepalive)检测的背景 对于TCP设计来说,如果一个客户端和服务器端建立连接后,不在进行数据传输,那么这个连接将会一直存在下去,理论上即使中间的路由器崩溃重启.或者中间的网络线 ...

  7. C++并行编程2

    启动线程 查看线程构造函数,接受一个返回值为void的函数.如下: void do_some_work(); std::thread my_thread(do_some_work); 也可以接受一个重 ...

  8. 【alpha】Scrum站立会议第4次....10.19

    小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app(约吧--暂定) 1.任务进度 2.燃尽图 功能列表 1.登录注册 2.创建跑步计划 3.筛选跑友 4.加一起跑步的人为好 ...

  9. 【Docker 命令】- create命令

    docker create :创建一个新的容器但不启动它 语法 docker create [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS同run命令 实例 使用 ...

  10. 第25天:js-封装函数-淘宝鼠标展示

    封装函数: 1.函数形参相当于变量,不能加引号. 2.实参要和形参一一对应. 案例:鼠标移到小图上,背景展示相应放大的图片.代码如下: <!DOCTYPE html> <html l ...