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. 小茜(xi)的减肥之路

    Description 今天,多年未见的小钧和小江在街头偶遇,小江想看看当年那个蠢蠢的小钧有没有变聪明一些,他灵机一动,说:“当初小茜立了个flag,说一定要减肥,她想着第一天跑一米,第二天跑两米,第 ...

  2. UVALive - 6869 Repeated Substrings 后缀数组

    题目链接: http://acm.hust.edu.cn/vjudge/problem/113725 Repeated Substrings Time Limit: 3000MS 样例 sample ...

  3. TCP 的有限状态机

    TCP 有限状态机的图中每一个方框都是 TCP 可能具有的状态. 每个方框中的大写英文字符串是 TCP 标准所使用的 TCP 连接状态名. 状态之间的箭头表示可能发生的状态变迁. 箭头旁边的字,表明引 ...

  4. ACM 第一天

    标签库元素: 队列<queue> FIFO 栈 <stack>  FICO 集合 set 不定长数组  vector 映射 map Maximum Multiple Time ...

  5. python学习笔记06:操作文件

    调用内置的open函数打开文件,传递两个参数:文件路径(绝对路径或相对路径),打开模式('r':读,'r+':读写,'w':写,'b':二进制): f = open('data.txt','w') f ...

  6. lintcode-36-翻转链表 II

    36-翻转链表 II 翻转链表中第m个节点到第n个节点的部分 注意事项 m,n满足1 ≤ m ≤ n ≤ 链表长度 样例 给出链表1->2->3->4->5->null, ...

  7. Swift-闭包理解(二)

    简明扼要的闭包表达式 其实Swift已经为我们提供了很多简化的语法,可以让我们保证代码的高可读性和维护性.还用上面的例子来说明,对于  greetPeople 这个全局函数来说,其实只需要使用一次,所 ...

  8. JS高级 1

    关于string,number是大写,那么就是构造函数,变量不可能为null值,除非手动设置,要解除对象的引用的时候手动去除. in关键字操作数组的时候操作的是索引值,不是里面的内容,.在操作对象的时 ...

  9. #Leetcode# 951. Flip Equivalent Binary Trees

    https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...

  10. (转)Linux常用性能检测命令

    一.uptime     Uptime命令的显示结果包括服务器已经运行了多长时间,有多少登陆用户和对服务器性能的总体评估(load average).load average值分别记录了上个1分钟,5 ...