Ada the Ladybug lives near an orange tree. Instead of reading books, she investigates the oranges. The oranges on orange tree can be in up to 5*50 Shades of Orange. She walks from orange to orange, examining different properties of orange tree. The oranges are connected by branches. There is more oranges then branches, yet it is still possible to get from any orange to any other orange [through branches]. The tree is rooted.

Ada has many questions in following form: She goes from orange A to orange B (by shortest path) and is interested in total number different Shades of Orange among all subtrees of all edges on shortest path.

Input

The first line of input consists of 1 ≤ T ≤ 100, the number of test-cases.

The first line of each test case contains three integers 1 ≤ N, Q ≤ 4.5*105, 0 ≤ R < N, the number of oranges, the number of questions and number of root.

Next line contains integers 1 ≤ Si ≤ 250, the shade of orange of orange i.

Next N-1 lines contains two integers 0 ≤ I, J < N, I ≠ J , the numbers of oranges which are connected by branch.

Next Q lines contains two integers 0 ≤ A, B < N, the path Ada is interested about.

The sum of all N and all Q among all test-cases won't exceed 106

Output

For each question answer the number of shades in all subtrees of all nodes on shortest path from A to B.

Example Input

1
10 7 1
1 2 1 4 5 6 6 8 9 9
0 9
9 3
3 4
4 6
4 5
4 8
1 3
1 2
2 7
4 4
8 6
0 6
7 0
7 2
0 0
2 3

Example Output

3
3
5
7
2
1
7

题意:给定一棵树,每个节点有一种颜色的橘子;Q次询问,每次询问,给出u、v,回答u到v的最短路径上的节点的子树一共有多少种颜色的橘子。

思路:其实就是问最小公共祖先LCA的子树的颜色种类。因为颜色只有250种,我们DFS时就用Bitset记录子树的颜色种类数。

#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<];
int fa[maxn][],dep[maxn],N,Q,rt,cnt;
bitset<>S[maxn];
void init()
{
for(int i=;i<=N;i++) S[i].reset();
for(int i=;i<=N;i++) Laxt[i]=;
memset(Laxt,,sizeof(Laxt));
cnt=;
}
void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int f){
fa[u][]=f; dep[u]=dep[f]+;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=f){
dfs(To[i],u);
S[u]|=S[To[i]];
}
}
}
void RMQ()
{
for(int i=;i<;i++)
for(int j=;j<=N;j++)
fa[j][i]=fa[fa[j][i-]][i-];
}
int LCA(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
for(int i=;i>=;i--)
if(dep[fa[u][i]]>=dep[v])
u=fa[u][i];
if(u==v) return u;
for(int i=;i>=;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[u][];
}
int main()
{
int T,x,u,v,lca;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&N,&Q,&rt);
rt++; init();
for(int i=;i<=N;i++){
scanf("%d",&x);
S[i][x]=;
}
for(int i=;i<N;i++){
scanf("%d%d",&u,&v);
u++; v++;
add(u,v); add(v,u);
}
dfs(rt,); RMQ();
while(Q--){
scanf("%d%d",&u,&v);
u++; v++;
lca=LCA(u,v);
printf("%d\n",S[lca].count());
}
}
return ;
}

SPOJ:Ada and Orange Tree (LCA+Bitset)的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  2. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  3. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  4. Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...

  5. 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree

    题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...

  6. BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树

    2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...

  7. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  8. 2588: Spoj 10628. Count on a tree

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5766  Solved: 1374 ...

  9. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 7669  Solved: 1894[Submi ...

随机推荐

  1. Objective C语言中nil、Nil、NULL、NSNull的区别

    以下内容是基于搜集整理的网上资料,供参考. nil:指向Objective C语言中对象的空指针,其定义值为(id)0. Nil:指向Objective C语言中类(Class)的空指针,其定义值为( ...

  2. Idea基本讲解、常用配置以及快捷键设置

    IDEA官网学习文档:https://www.jetbrains.com/idea/documentation/ IDEA官网入门教程:https://www.jetbrains.com/help/i ...

  3. ubuntu 14.04安装nodejs

    http://stackoverflow.com/questions/32902699/cannot-install-ember-on-ubuntu-1404/33495134

  4. STL algorithm源代码:stl_algo.h

    <span style="font-size:18px;">// Algorithm implementation -*- C++ -*- // Copyright ( ...

  5. Android Dynamic Action(动态Action)—像访问网页一样地访问Activity

    Android Dynamic Action,简称DA,是一种简便.可变Action的实现方案.DA框架的初衷是为了取代Context.startActivity的调用方式,使用建造者模式(Build ...

  6. 【转载】C# 理解泛型

    术语表 generics:泛型type-safe:类型安全collection: 集合compiler:编译器run time:程序运行时object: 对象.NET library:.Net类库va ...

  7. 移动GPU渲染原理的流派——IMR、TBR及TBDR

    移动GPU渲染原理的流派--IMR.TBR及TBDR 移动GPU相对桌面级的GPU仅仅能算是未长大的小孩子,尽管小孩子在某些场合也能比成人更有优势(比方杂技.柔术之类的表演).但在力量上还是有先天的区 ...

  8. 全卷积神经网络FCN理解

    论文地址:https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf 这篇论文使用全卷积神经网络来做语义上的图像分割,开创了这一领 ...

  9. (转)OutOfMemory时抓取heap 快照

    转自:https://testerhome.com/topics/579 首先说一下,在程序没有崩溃的时候如何抓取heap快照.这个大家应该都知道,在ddms中自带此功能.   见上图首先我们选中一个 ...

  10. ID--HANDLE--HWND三者之间的互相转换

    利用PreTranslateMessage,响应按钮控件的按下(WM_LBUTTONDOWN)和松开(WM_LBUTTONUP)   VC的button控制只有两个事件,一个是单击事件,一个事双击事件 ...